Request with file in body (REST-API)

@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