cancel
Showing results for 
Search instead for 
Did you mean: 

How do I send an HTTP multi-part POST request from within XS?

Former Member
0 Kudos

Hi,

I am trying to send an HTTP mulit-part POST request from within XS, but cannot seem to get it to work.  The code is sending a jpeg image (imageBlob is an ArrayBuffer read from the database) to be OCRed.  If working properly,  the sample code should return an error that the apikey is not valid.

Below is what I have tried, though based on the response, it seems not to send the language parameter.


var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST, "");

request.parameters.set("language", "en");

request.parameters.set("apikey", "xxxxxxxxxx");

var imageEntity = request.entities.create();

imageEntity.contentType = "image/jpeg";

imageEntity.headers.set("name", "image");

imageEntity.setBody(imageBlob);

var response = client.request(request, "http://api.ocrapiservice.com/1.0/rest/ocr","http://proxy.wdf.sap.corp:8080").getResponse();

rawOCRText = response.body.asString();

As an alternate approach, I tried to send the parameters also as entities, but I get the same result:


var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST, "");

var imageEntity = request.entities.create();

imageEntity.contentType = "image/jpeg";

imageEntity.headers.set("name", "image");

imageEntity.setBody(imageBlob);

var languageEntity = request.entities.create();

languageEntity.headers.set("name", "language");

languageEntity.setBody("en");

var keyEntity = request.entities.create();

keyEntity.headers.set("name", "apikey");

keyEntity.setBody("xxxxxxxxxx");

var response = client.request(request, "http://api.ocrapiservice.com/1.0/rest/ocr","http://proxy.wdf.sap.corp:8080").getResponse();

rawOCRText = response.body.asString();

If I try the first approach without adding the image entity and lanaguage parameter, I get the same result:


var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST, "");

request.parameters.set("apikey", "xxxxxxxxxx");

var response = client.request(request, "http://api.ocrapiservice.com/1.0/rest/ocr","http://proxy.wdf.sap.corp:8080").getResponse();

rawOCRText = response.body.asString();

However, if I only have the image entity removed, I get a result which indicates the language parameter is OK and only the image entity is missing:


var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST, "");

request.parameters.set("language", "en");

request.parameters.set("apikey", "xxxxxxxxxx");

var response = client.request(request, "http://api.ocrapiservice.com/1.0/rest/ocr","http://proxy.wdf.sap.corp:8080").getResponse();

rawOCRText = response.body.asString();

This seems to indicate that something is not working properly when I try to add the entity.  Any help would be most appreciated.

Thanks,

David

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi David,

We used the following solution, file.content is a Blob value

..........

var client = new $.net.http.Client();

var request = new $.net.http.Request($.net.http.POST,  "/api/mail.send.json");

request.contentType = "multipart/form-data";

request.parameters.set("html", data.html);

request.parameters.set("subject", data.subject);

     .......

if (data.files) {

     for ( var fileKey in data.files) {

          var file = data.files[fileKey];

          var entity = request.entities.create();

         

          file.cid && request.parameters.set("content[" + file.name + "]", file.cid);

         

          entity.headers.set('Content-Disposition', 'form-data; name="files[' + file.name + ']"; filename="' + file.name + '"');

         

          file.contentType && entity.headers.set('Content-Type', file.contentType); entity.setBody(file.content);

     }

}

client.request(request, destination).getResponse();

Alex