Outbound Rest Post body in Jakarta

xstaffelbach
Kilo Contributor

I simply want to send a POST message to a server. I have a GET working from that same server already on SNOW.

I just can't figure out how to send the request body.

For instance I want to mimic this curl statement

curl -H "Content-Type: application/json" -X POST -d '{"title":"hi"}' http://live_server:5000/api/v1/incident

Where do I enter the {"title":"hi"}?

Screen Shot 2017-08-15 at 1.12.21 PM.png

This image shows the UI that I am dealing with, it looks different from the tutorial UI's so I am having trouble using them.

I cannot find the Request Body field

1 ACCEPTED SOLUTION

You need to send a string in the request body.



Change these two...


var body = {"title":"hi"};


r.setRequestBody(body);



To this:


var body = '{"title":"hi"}';


r.setRequestBody(body);



or


var body = {"title":"hi"};


var bodyText = JSON.stringify(body);


r.setRequestBody(bodyText);


View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

Hi Matthew,



You'll have to call this REST message via script. Once you set up the sn_ws.RESTMessageV2() object, use the setRequestBody() method with the stringified JSON object you want to pass.



https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_RMV2-setRequestBody_String_body



Lots of good examples on Live Coding Happy Hour.



ServiceNow Dev Program - YouTube


Let me know if I have answered your question or if there is still something you need on this one.


Right now I have a business rule that calls the outbound rest service which can see in my picture above.


The Business rule's code is below and the condition is set as blank.


I can call this business rule and see the message sent by the business rule.


But I cannot see this code's output and the server does not seem to have received the POST successfully.


Can you see an issue with my code? If not, how do I debug?



try {


var r = new sn_ws.RESTMessageV2('Flask speaker', 'new ticket');



//override authentication profile


//authentication type ='basic'/ 'oauth2'


//r.setAuthentication(authentication type, profile name);


// curl -i -H "Content-Type: application/json" -X POST -d '{"title":"hi"}' http://my_server.net/api/v1/incident



var body = {"title":"hi"};


r.setRequestBody(body);


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


}


catch(ex) {


var message = ex.getMessage();


}


For some more context on my intent here is a question that I asked earlier: send POST request when a business rule is activated