0

We are trying to POST a document to GeoNode through its REST API. Here is the code. The error returned is that JSON parse error.

I have tried many ways of sending the fileObject - a string, blob and file, but the same error pops up. Anyone can advice me what could be the issue?

    //now to send the document upload in geonode
    let url = 'https://DOMAIN/api/v2/documents/';                
    var crf_token1 = $('[name="csrfmiddlewaretoken"]').attr('value');
    
    var fileK = document.querySelector('#fileInput');  // the geojson file we uploaded
           
    let headers = new Headers();
    //headers.set('Authorization', token.token_type + ' '  + token.access_token);  //+ ' ' +
    headers.set('Content-Type','application/json');  
    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', fileK.files[0]); //jsonobject); //jsonobject); //fileK.files[0]); // read.result ); // blobD read.result //fileD); //fileF) //myFile) //fileX) //myTestFile) //myEncryptedFile) //global_finalEncryptedZipFile)       
     //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');         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)'}
2
  • I even tried to use a file object by hard coding it. But still does not work. var f = new File(["My test file contents as a string"], "myTestFilename.txt");
    – SharmaPN
    Feb 25, 2022 at 12:55
  • Some say its the issue with the Content-type header. But when I tried to change it to 'multipart/form-data' different errors came up. Those may be related to the Accept header. I would really appreciate the readers to please try the code.
    – SharmaPN
    Feb 25, 2022 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.