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
httpMain.open(RequestType, 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 {
httpMain.send(jsonData);
...
} catch(e) {
...
}
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.
Here’s the code I use to upload a binary file to a server:
var params = { baseURL:"http://example.com:8080", 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");
xhr.setTimeouts(30000,30000,0,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);
xhr.open("POST", this.baseURL + endPoint, false);
if(this.token) xhr.setRequestHeader("auth_token", this.token);
xhr.setRequestHeader("Content-Type", this.MIMEType);
xhr.send(bStream);
bStream = null;
return xhr;
}
openXHRStream = function(fName){
// Open a stream with XMLHTTP
var xhr2 = new ActiveXObject("Msxml2.XMLHTTP.6.0");
xhr2.open("GET", "file:///" + fName, false);
xhr2.send();
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.