File API - works with cURL but fails with AJAX and Postman

743 Views Asked by At

I am running gotenberg as my document to PDF conversion API and it is working with cURL.

The cURL command looks like this

curl --request POST --url http://example.com  --header 'Content-Type: multipart/form-data' --form [email protected] > result.pdf

The API works only with cURL, When i try hitting the same API with Postman or AJAX i get a response but on saving the details or by previewing the response using Postman i get an empty PDF file.

My AJAX request looks like this

  var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}

Can anyone throw some light as to what is happening with my request?

I get the below headers with my response but the created files are empty

enter image description here

1

There are 1 best solutions below

0
Nithin On BEST ANSWER

I needed to handle blob response and thus my AJAX call must have looked like this.

var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        "xhr": function(){
               var xhr = new XMLHttpRequest();
               xhr.responseType= 'blob'
               return xhr;
        },
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}