REST API POST Method Script is not working HTTP error 500 & 400 dependent on Method content.

ashwanikumar
Tera Expert

Hi All,

I am trying to communicate to third party tool using REST API POST & GET Method. (I am working on a scoped application.)

I created a connection by creating a new REST Message using an End Point. and i am able to use POST Method to create a request on third party tool by adding content on the POST request.

{

  "transaction_type" : "NewUser",

  "correlation_id"   : "REQ00001",

  "requester_id"   : "000089",

  "account_type"   : "E-mail Account",

  "business"   : "Sales",

  "business_area"   : "Finance",

  "department"   : "IT",

}

I get HTTP Status 201 and system is able to create a request on 3rd party tool.

When I clicked on Auto-Generate Variables, it did not create new variables. (I expected system will automatically create variables.) I created new variables on Variable Substitutions related list.

NameEscape TypeTest Value
transaction_typeNo escapingNewUser
correlation_idNo escapingREQ00001
requester_idNo escaping000089
account_typeNo escapingE-mail Account
.................

I Previewed Script Usage and used on a workflow script to use POST Method. But I am not getting response (var responseBody = response.getBody(); returns nothing).

When I keep the POST Method content empty, i get 500 HTTP Status when executed from script.

If i keep the content with the values, i get 400 HTTP Status when executed from script.

I am not able to understand when I am calling the POST method using script, why is HTTP Status dependent on the method content. while executing, it must pass the values   which i pass from script   r.setStringParameterNoEscape('requester_id', requester_id);

Should i keep the content field empty or   should i define the content using the variables which i created on related list. e.g. { "transaction_type" : ${transaction_type},.......... }

Please let me know where am i doing wrong..

Thanks,

KUMAR

2 REPLIES 2

Gaurav Bajaj
Kilo Sage

Hi Kumar,



Try logging the formed request body first and take it forward from there.


r.getRequestBody(); //not sure of the syntax.



If you are setting the variables from script,


r.setStringParameterNoEscape('requester_id', requester_id)



Then you must keep the value of variable inside ${}



{


  "transaction_type" : "NewUser",


  "correlation_id"   : "REQ00001",


  "requester_id"   : ${requester_id},


  "account_type"   : "E-mail Account",


  "business"   : "Sales",


  "business_area"   : "Finance",


  "department"   : "IT",


}




Please mark correct/helpful based on the impact of the response.




Thanks


Gaurav


Singareddy Anil
Giga Contributor

Hi AshwaniKumar,

 

If you were able to find solution for your issue. Please share me the process how to do POST REST in servicenow.

My req is to insert record in third party tool, whenever a record is inserted in servicenow. If possible share me the scripting.

 

Thanks in advance