Skip to Content
0

XSJS - POST HttpRequests to external not working

Feb 11 at 07:46 AM

80

avatar image

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

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

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

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

1 Answer

Paul Aschmann
Feb 12 at 09:07 PM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hi Paul,

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

0