Skip to Content
author's profile photo Former Member
Former Member

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

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Jan 27, 2014 at 08:27 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.