POST Request with pdf in body form-data


I want to send a PDF from workflow to a server (RESTful-API), but get status 400…
I call the API in Postman is work.
Thanks for any help! :pleading_face:

my code:'POST',POST_API,false);
XHR.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary+';');
XHR.setRequestHeader('Accept-Encoding', 'gzip, deflate')


payload data be send:

Content-Disposition: form-data; name="file"; filename="test.pdf"
Content-Type: application/pdf

<PDF Binary.....>

Here is a snippet of code that I use:

RequestType - The type of request to send (e.g. GET, POST)
URL - The endpoint URL for the API
jsonData - The JSON to send to the endpoint

This looks all very similar to what you use but maybe it can still help. I have removed the authentication part as I use a username + password for my use cases.

var httpMain = new ActiveXObject("MSXML2.ServerXMLHTTP.6.0");
httpMain.setOption(2, 13056);			// SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS, URL, false);
httpMain.setRequestHeader("Content-Type", "application/json");
// Return JSON so we can use it in our process
httpMain.setRequestHeader("Accept", "application/json");
httpMain.setRequestHeader("Connection", "close");

try {
		} catch(e) {

Thanks for your reply. But it still not work.:sob:

Status 400 is a bad request so then I would look at your payload as not being correct somehow. Unfortunately I will not be able to help with that as my only experience is with adding a PDF as base64 data to a JSON body, as that is what we use ourselves to transfer information.

1 Like

What does the Connect Server log say?

Here’s the code I use to upload a binary file to a server:

var params = { baseURL:"", MIMEType:"application/pdf" } ;
var restClient  = new RestClient( params );
var xhr =  restClient.uploadFile( "/uploadPDF2", Watch.GetJobFileName() )

if(xhr.status>=200 && xhr.status<300) {
  Watch.log("File uploaded successfully",2);
} else {
  Watch.log("Error " + xhr.status + " while uploading file",1);

function RestClient(conf){
  var xhr       = new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");

  this.baseURL  = conf.baseURL;
  this.token    = conf.token;
  this.MIMEType = conf.MIMEType || "application/octet-stream";

  this.uploadFile = function(endPoint, fName){
    fName = fName.replace(/\\/g,"/");
    var bStream = openXHRStream(fName);"POST", this.baseURL + endPoint, false);
    if(this.token) xhr.setRequestHeader("auth_token", this.token);
    xhr.setRequestHeader("Content-Type", this.MIMEType);
    bStream = null;
    return xhr;

  openXHRStream = function(fName){
    // Open a stream with XMLHTTP
    var xhr2 = new ActiveXObject("Msxml2.XMLHTTP.6.0");"GET", "file:///" + fName, false);
    return xhr2.responseStream;

The code uses an HTTPRequest.responseStream to upload the file, thereby ensuring that the data is not transformed in any way. This method can be used for any type of file. Ideally, you would always set the mime type to the appropriate value, but even if you don’t, then the data is uploaded as application/octet-stream which instructs the server on the receiving end to treat the file as binary data, thus preventing any kind of transformation from occurring on the server.


Thanks for your help.
I tried the code in my workflow,but still get the reponse 400 …
I think maybe there is something wrong when I upload my pdf file?

As @dvdmeer pointed out, a 400 response means the server doesn’t understand the request. You will have to look at the server’s REST API documentation to figure out what it expects to be included in the request, and how it should be formatted. Each REST server has its own set of rules, so you have to make sure to follow them.

For instance, the code I posted in my previous reply works flawlessly with OL Connect’s REST API, but only if you include the token parameter. If you omit the token, then the call fails. There might be a similar requirement with the REST server you’re trying to contact.

1 Like

I finally found the problem,some parameter in the request have to be encoded. :open_mouth:
Thank u :slightly_smiling_face:

1 Like