cancel
Showing results for 
Search instead for 
Did you mean: 

XSJS - POST HttpRequests to external not working

former_member197071
Participant
0 Kudos

Hi,

I am trying to send an email from XSJS using HttpRequest POST method. I am trying from a month, googling, searching on sap answers portal but still could not get it working.

This is a working instance from Postman which I want to integrate with XSJS.

I tried to get this done with multiple ways, the last one is this:

var req =new $.net.http.Request($.net.http.POST,"/xxxx/xxxxx/sendmail.php");

var entity = req.entities.create();
req.contentType = "multipart/form-data";

body = body + "Content-Disposition: form-data; toEmail=\"ankurxxaa@aasc.com\"\r\n";
body = body + "Content-Disposition: form-data; subject=\"Sample Email test\"\r\n";
body = body + "Content-Disposition: form-data; rawBody=\"Raw Text body\"\r\n";
body = body + "Content-Disposition: form-data; htmlBody=\"<p>HTML Body</p>\"\r\n";
body = body + "Content-Disposition: form-data; key=\"myapikey\"";
  
entity.headers.set("Content-Disposition", "form-data; toEmail=\"ankurxxaa@aasc.com\"\r\n");
 entity.headers.set("Content-Disposition", "form-data; subject=\"Sample Email test\"\r\n");
entity.headers.set("Content-Disposition", "form-data; rawBody=\"Raw Text body\"\r\n");
entity.headers.set("Content-Disposition", "form-data; htmlBody=\"<p>HTML Body</p>\"\r\n");
entity.headers.set("Content-Disposition", "form-data; key=\"myapikey\"\r\n");
  
  //entity.setBody(body);
  req.setBody(body);
  
  var clientresponse = client.request(req, dest).getResponse();
  $.response.status = $.net.http.OK;
  $.response.setBody(clientresponse.body.asString());

And this is how it is flowing if we run from cURL:

curl -X POST \
  http://aabbccddeeff/xxxx/yyyy/sendmail.php \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: ec19e174-ac78-f9a7-ade6-cef762b4e53f' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F toEmail=ankurasde@3vr3gf.com \
  -F 'subject=Test Mail' \
  -F 'rawBody=This is test mail' \
  -F 'htmlBody=<p>This is <strong>test</strong> mail.<p>' \
  -F 'myapikey'

It keeps on giving me error message as Payload is empty. The endpoint is based on PHP and it says

if(empty($_POST))
{
die("Payload is empty")
}

Need help from SAP experts.

Regards,

Ankur

paschmann
Advisor
Advisor
0 Kudos

Hi Ankur, I assume that your CURL POST is working as expected?

Accepted Solutions (0)

Answers (1)

Answers (1)

paschmann
Advisor
Advisor
0 Kudos

Also - I am not sure if you are only using PHP to send emails, but you can configure your HANA/XS instance to do the same. If you are interesting, check out this GIST: https://gist.github.com/paschmann/144fea92015428bd773adfee290ca637

former_member197071
Participant
0 Kudos

Hi Paul,

Thanks for your response. I am trying to send an email in this example, will test your code.