Skip to Content
0

POSTING a ZIP file to REST API using Outbound XSJS service

Aug 28, 2017 at 11:53 AM

115

avatar image
Former Member

Hi,

I was trying to call a REST API (http://sandbox.api.sap.com/ml/topicdetection/inference_sync) that accepts a ZIP file and a string as form-data, below is my code,

try { 
var destination = $.net.http.readDestination("package", "httpdestfile"); 
var client = new $.net.http.Client(); 
var zip = new $.util.Zip(); 
zip["file1.txt"] ='The file contains this data'; 
zip["file2.txt"]='the second file containing the data'; 
var req = new $.net.http.Request($.net.http.POST,"");

req.setBody(JSON.stringify("options":{ "numTopics":2, "numTopicsPerDoc":1, "numKeywordsPerTopic":5 }));

var resp = client.request(req, destination).getResponse();
if(resp.body) 
try{ myBody = JSON.parse(resp.body.asString()); 
} 
catch(e)
{ myBody = resp.body.asString(); 
}
$.response.contentType = "application/json"; 
$.response.setBody(JSON.stringify({"body" : myBody , "Headers" : myHeader })); 
}
catch (e) 
{
$.response.setBody(e.message); $.response.status = $.net.http.INTERNAL_SERVER_ERROR; 
}

i was able to set the headers for the REST service but not the body as setBody doesn't seem to work,Also i was trying to create a zip file in the same xsjs code and post it to the rest service

I have attached the snap of the output that i get after executing the above code, you can see that even if i pass "options" using setBody, the message says options are missing,

Can any one please help on acheiving this.

Thanks

Sumeet

capture.png (62.1 kB)
10 |10000 characters needed characters left characters exceeded

Do you get any errors or something else which let you "assume" that it does not work? If yes, please add the information to your question.

Regards,
Florian

0
Former Member

Hi Florian,

I have added few more details and have attached a snap specifying the error

Let me know if i need to add any more details

Thanks ,

Sumeet

0
* Please Login or Register to Answer, Follow or Comment.

0 Answers