Request with file in body (REST-API)

Hello,

I need to send a xml file via http request in Workflow. I can not find a way to get it work, because the activexobject seems to not support DataForm object.

In Postman I tried different approaches to send file and there I have to select “form-data” in “Body” with “file” as key type and then I have to select the xml file as value.

Any help would be appreciated.

Thought I could help you understand how Postman will send a request like your screenshot. It’s basically a multipart/form-data where the file is enconded in a special format, multiple files will be delimited by a boundary which is automalically calculated by postman.

Do you want to send an XML to the workflow from a client (like postman) or do you want to send from the workflow to a client?

Thank you for your response.

I want to send a XML from workflow to a server (REST-API).
If I send the XML file base64 encoded with header “multipart/form-data” I get an error from the REST-API server.

Actually I can manage the request through curl request (VBS WScript.shell command line execution) in workflow. But in curl I do not get the response properly. As response I get nothing (.exec()) or 0 (.run()). But on the target server I can see that my request was successfully sent.

My curl command:

var xml_file = '"/C:/PLANETPRESS/myFile.xml"';
"curl --location 'http://xxx.xxx.xxx.xx/backendserver/restapi/upload' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Authorization: Bearer "+token+"' --form '" + xml_file + "'"

So I would really prefer a JS solution in workflow script to check if the request was successfully send.

Edit: It seems, that the curl request does not work as expected. So I need another approach.

@Maxiride based on your good hin to Postman I tried it again in workflow JS script (see below) and now it works.

FYI my working code:

var token = Watch.GetVariable("token");
var xml_name = Watch.GetVariable("xml_name"); //e.g.: myFile.xml
var xml_content = Watch.ExpandString("%c"); //XML is my input file in workflow process
var target = Watch.GetVariable("REST_SERVER"); //e.g.: http://localhost/endpoint/restapi
var url = target + "/upload";
var http = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
try {
	http.open("POST",url,false);
	http.setRequestHeader("Authorization", "Bearer " + token);
	http.setRequestHeader("Content-type", "multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxx;");
	var formBody = "----xxxxxxxxxxxxxxxxxxxx" + "\r\n" +
	"Content-Disposition: form-data; filename=/" + xml_name + "\r\n" +
	"Content-Type: text/xml" + "\r\n" + "\r\n" +
	xml_content + "\r\n" +
	"----xxxxxxxxxxxxxxxxxxxx" + "\r\n" + "\r\n";
}
catch(error){
	Watch.Log("ERROR: '" + (error.number & 0xFFFF).toString() + "' with Description: '" + error.description +"'",1); //DEBUG
}

http.send(formBody);

Watch.Log('response text: ' + http.responseText,2); //DEBUG
Watch.Log('response status: ' + http.status,2); //DEBUG

PS: Replace ----xxxxxxxxxxxxxxxxxxxx with any custom string (in all places).

1 Like

Happy you managed to get over the issue! Thanks for sharing the code snippet, I think I will reause it soon in anothor project!