How to make a REST API call through a MID Server?

Smith Johnson
Tera Guru

Hello all,

I have the following REST API call inside my script include.

However, instead of this call, I want to configure a call to the third system through a MID server.

At first I created a new MID Server on my instance with all the needed information (name "myMID")

How can I use this MID server inside my code?

My code is the following:

var r=new sn_ws.RESTMessageV2();

//endpoint
r.setEndpoint("aURL");   //set a url endpoint (third system's endpoint)

r.setHttpMethod("post");  //HTTP method

//set the queryParameters (the first parameter is the name of the parameter that the third system is waiting for)
r.setQueryParameter("id",id);      //id is passed from catalog client script
r.setQueryParameter("t",t); //t is passed from catalog client script

var response=r.execute();    //send the REST API call
var responseBody=response.getBody();   //receive back the response


var answer=JSON.parse(responseBody);

 ...

 

How can send this call from the MID server??

1 ACCEPTED SOLUTION

Hi,

Points below

You are not able to connect to that API from Postman since it is in client's network

1) I configured the MID server on my instance and I put it in the http request tab of an outgoing message. Do I need to make anything else?

- The MID Server should be in client's internal network and you should check by hitting the endpoint of API when you login to MID Server to confirm if mid server can reach. If not get client's network team involved and allow MID Server machine's IP address to be reaching to that client API

2) I guess I need somehow to install the mid server on the client's network? Am I right?

- Yes

3) To make the call, I just need to use the outbound message that uses the mid server?

- Yes; ensure you first test the endpoint from mid server

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

45 REPLIES 45

@Smith Johnson 

If you are using mid server then you can either use execute() or executeAsync() as both are same.

Don't use waitForResponse()

Reference: check this link &it should explain the use-case and best practices

Outbound REST Web Services RESTMessageV2 and SOAPMessageV2 execute() vs executeAsync() Best Practice...

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

Trying to do a test of the call, I have the following code inside scripts-background.

var t="R";
var id="E29200000555";
var r=new sn_ws.RESTMessageV2();

//endpoint
r.setEndpoint("A URL"); 
			
r.setHttpMethod("post");  //method
	
			
//Set Headers
r.setRequestHeader('Content-Type','application/json');
			
var VALUES=[];   //array of object
var inputObj={
   "t":t,
   "id":id
};

VALUES.push(inputObj);  //add the object to the array VALUES
//stringify the array before senting it to the third system
var requestBody=JSON.stringify(VALUES); 
gs.info("REQUEST BODY IS "+requestBody);
			
//set the request body with content the stringified array
r.setRequestBody(requestBody);
			
//set a MID server
r.setMIDServer("mymid");

var response=r.execute();    //send the REST API call 
responseBody=response.getBody();
gs.info("RESPONSE IS "+responseBody);

What I get back, is the following:

find_real_file.png

Does it mean that I have an error on the call or on the MID server?? The response is NULL, so I guess that I have an error, but I can not understand where. What ecc_queue0003 supposed to be?

 

Ankur really thank you for your patience and your diligence to help me. I appreciate it a lot.

Hi,

search ecc_queue table with type as output and input and you can see the response

check the http status code to confirm any error

var response=r.execute();    //send the REST API call
responseBody=response.getBody();
gs.info("RESPONSE IS "+responseBody);
gs.info("HTTP Status IS"+ response.getStatusCode());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

Ankur I receive: HTTP Status IS 0

If I go to Outbound HTTP log, I have the following message response to my call:

[ERROR CODE: -1 ] The request failed: org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 10000 ms when posting to ...

Any ideas how I can fix it within my code?

@Smith Johnson 

It means some issue in connecting to the API via the mid server

Did you test the API endpoint from MID Server machine first?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader