通过 REST API 发布文档会返回 JSON 解析错误
我正在使用 REST API 将文档发布到 GeoNode。这是 JavaScript 代码。返回的错误是 JSON 解析错误。我尝试了多种发送 fileObject 的方法 - 字符串、blob 和文件,但会导致相同的错误。不确定可能是什么问题。
我在这里发布这个问题的原因是因为这根本不是 GeoNode 问题 - 如果这些错误出现在 JavaScript 控制台中,那么我还无法将 POST 有效负载发送到 GeoNode。因此,这是在浏览器端执行的 JavaScript 代码中的错误。
有人说这是 Content-type 标头的问题。但是当我尝试更改它时,出现了不同的错误。这些可能与 Accept 标头有关。 我非常感谢读者尝试一下代码。
//now to send the document upload in geonode
let url = 'https://DOMAIN/api/v2/documents/';
var crf_token1 = $('[name="csrfmiddlewaretoken"]').attr('value');
var file = new File(["My test file contents as a string"], "myTestFilename.txt");
let headers = new Headers();
//headers.set('Authorization', token.token_type + ' ' + token.access_token); //+ ' ' +
headers.set('Content-Type','multipart/form-data');
headers.set('Accept','application/json');
var crf_token = $('[name="csrfmiddlewaretoken"]').attr('value');
headers.set('X-CSRFToken', crf_token);
var formData = new FormData();
formData.append('file', file);
//document related parameters
formData.append('owner', 'admin'); formData.append('title', 'testdoc');
formData.append('abstract', 'testdoc'); formData.append('attribution', 'test');
formData.append('bbox_polygon', 'MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)))');
formData.append('constraints_other', 'no'); formData.append('data_quality_statement', 'no');
formData.append('doi', 'test'); formData.append('embed_url', 'test');
formData.append('is_approved', 'true'); formData.append('is_published', 'true');
formData.append('ll_bbox_polygon', 'MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)))');
formData.append('maintenance_frequency', '1'); formData.append('metadata_only', 'true');
formData.append('popular_count', '1'); formData.append('share_count', '1');
formData.append('srid', '1'); formData.append('purpose', '1');
formData.append('rating', '1');
formData.append('language', 'English'); formData.append('supplemental_information', 'No information provided');
formData.append('temporal_extent_end', '2022-02-02 12:21'); f
formData.append('temporal_extent_start', '2022-02-02 12:21');
formData.append('thumbnail_url', 'test'); formData.append('Regions', 'Global');
formData.append('Responsible', 'admin'); formData.append('date', '2022-02-02 12:21');
formData.append('edition', 'test'); formData.append('date_type', 'test');
fetch(url,
{
method:'POST',
credentials: "same-origin",
headers: headers ,
body: formData // jsonobject //formData //fileK.files[0]
}
)
.then(response => response.json())
.then(json => console.log(json));
当我尝试上传 Word 文档时,浏览器控制台返回的错误为
{detail: "JSON parse error - 'utf-8' codec can't decode byte 0xd2 in position 226: invalid
continuation byte"}
当我尝试上传 geojson 文件时,返回的错误为
{detail: 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
I am using the REST API to POST a document to GeoNode. Here is the Javascript code. The error returned is that JSON parse error. I have tried many ways of sending the fileObject - a string, blob and file, but results in the same error. Not sure what could be the issue.
The reason I am posting this question here is because this isn't a GeoNode issue at all - if these errors appear in the JavaScript console then I am not able to send the POST payload to GeoNode yet. Thus, it is a error in the JavaScript code which is executing at the browser end.
Some say its the issue with the Content-type header. But when I tried to change it, different errors came up. Those may be related to the Accept header.
I would really appreciate the readers to please try the code.
//now to send the document upload in geonode
let url = 'https://DOMAIN/api/v2/documents/';
var crf_token1 = $('[name="csrfmiddlewaretoken"]').attr('value');
var file = new File(["My test file contents as a string"], "myTestFilename.txt");
let headers = new Headers();
//headers.set('Authorization', token.token_type + ' ' + token.access_token); //+ ' ' +
headers.set('Content-Type','multipart/form-data');
headers.set('Accept','application/json');
var crf_token = $('[name="csrfmiddlewaretoken"]').attr('value');
headers.set('X-CSRFToken', crf_token);
var formData = new FormData();
formData.append('file', file);
//document related parameters
formData.append('owner', 'admin'); formData.append('title', 'testdoc');
formData.append('abstract', 'testdoc'); formData.append('attribution', 'test');
formData.append('bbox_polygon', 'MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)))');
formData.append('constraints_other', 'no'); formData.append('data_quality_statement', 'no');
formData.append('doi', 'test'); formData.append('embed_url', 'test');
formData.append('is_approved', 'true'); formData.append('is_published', 'true');
formData.append('ll_bbox_polygon', 'MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)))');
formData.append('maintenance_frequency', '1'); formData.append('metadata_only', 'true');
formData.append('popular_count', '1'); formData.append('share_count', '1');
formData.append('srid', '1'); formData.append('purpose', '1');
formData.append('rating', '1');
formData.append('language', 'English'); formData.append('supplemental_information', 'No information provided');
formData.append('temporal_extent_end', '2022-02-02 12:21'); f
formData.append('temporal_extent_start', '2022-02-02 12:21');
formData.append('thumbnail_url', 'test'); formData.append('Regions', 'Global');
formData.append('Responsible', 'admin'); formData.append('date', '2022-02-02 12:21');
formData.append('edition', 'test'); formData.append('date_type', 'test');
fetch(url,
{
method:'POST',
credentials: "same-origin",
headers: headers ,
body: formData // jsonobject //formData //fileK.files[0]
}
)
.then(response => response.json())
.then(json => console.log(json));
When I try to upload a word document, the error returned within the browser console is
{detail: "JSON parse error - 'utf-8' codec can't decode byte 0xd2 in position 226: invalid
continuation byte"}
When I try to upload geojson file, the error returned is
{detail: 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论