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

Thanks for the sharing of the article and your knowledge too.

What I am trying to do is the following:

 

I have a record producer which has some fields. When the user enters a value on "ID" field, then a catalog client script sends the "ID" field value (let's say 123456789) to the script include. Within my script include I make a post call to the third system through a mid server and I am waiting for a response. Then this response is sent back to the catalog client script and auto-populates some other form fields.

Does the execute() method seems logical to my case?

 

Thank you so much for your patience and all the knowledge that you keep sharing with me.

 

Yes, execute method is the way to go as you need to process the response and display the data on the current form and until you get the response it will keep on running. On the other hand, the problem with executeAsync will be that the call will be added to queue and until it get resources to complete the call, you will not recieve any response and no data will be displayed.
Regards,
Muhammad

Hitoshi Ozawa
Giga Sage
Giga Sage

Trying to do a third party api validation on a form isn't really that good because it reduces the performance of the system. It may be OK if there's not too many users using it concurrently but if number of users are high, the system performance will degrade.

Normally, instead of doing a validation within a form or on onSubmit, a flow is created that is triggered when  the form is submitted. The flow runs in the background and validates the content with the third party api and updates the status of the form and notifies the end-user if there is some invalid content. This design will allow more concurrent users to submit a form without performance penalty because the validation is running in the background queue. 

https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/administer/flow-designer/task/re...

 

Hey @Hitoshi Ozawa,

thank you for your replies.

What do you mean by saying "trying to do a third party api validation on a form" ?

What I want to achieve is form fields to be autopopulated once the user enters a value on a field form.

Being more precise, if the user enter on field "A" a value, then a rest api call is made to the third party and with the response of the third system, another field (let's say field "B") is autopopulated.

Hitoshi Ozawa
Giga Sage
Giga Sage

Looking at the code in the question, it seems it was created from scratch. I usually let ServiceNow generate the REST code for me by using REST Message.

From there, it's possible to test connection and also generate source code to paste into my script.

find_real_file.png

When I'm need to pass through MID server to access internal api, I just need to specify it from the page. (Note, in generated code, I'll have to manually enter the MID server name.)

find_real_file.png

 

If the connection from REST Message page fails, it'll mean there's some problem with being able to access the api in the internal system. Login to the MID server and try accessing the API from a web browser or a command on the MID server. Since the MID server is within the internal network, not being able to access the api from the MID server would imply there is something wrong with the network setting or a firewall setting on the api server.