Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set order of HTTP query parameters in outbound REST call?

MG Casey
Mega Sage

A particular outbound REST API that I am calling seems to care about the order of my parameters in the URL.

I've tried to control this in 2 different ways:

Method #1: Adding query parameters in my desired order through my script:

var incontactREST = new sn_ws.RESTMessageV2('inContact_API', 'get');

incontactREST.setStringParameter("authorization", token);

incontactREST.setStringParameter('url', 'services/v11.0/agents/' + agentNum + '/statehistory');

incontactREST.setStringParameter('contentType', 'application/x-www-form-urlencoded');

incontactREST.setQueryParameter("startDate", datestart);

incontactREST.setQueryParameter("endDate", dateend);

incontactREST.setQueryParameter("mediaTypeId", "0");

incontactREST.setQueryParameter("outboundStrategy", "0");

incontactREST.setQueryParameter("updatedSince", updatedSince);

However, this still results in the following URL:

services/v11.0/agents/2471149/statehistory?mediaTypeId=0&outboundStrategy=0&endDate=2018-02-14T00:00:00-0600&updatedSince=2018-02-13T11:19:33-0600&startDate=2018-02-13T00:00:00-0600

  • I'm not quite sure how's sorting those parameters in the URL. They're definitely not alphabetical by label, nor alphabetical by value.

Method #2: Doing variable substitution, then setting the Order field on each query parameter.

find_real_file.png

var incontactREST = new sn_ws.RESTMessageV2('inContact_API', 'get');

incontactREST.setStringParameter("authorization", token);

incontactREST.setStringParameter('url', 'services/v11.0/agents/' + agentNum + '/statehistory');

incontactREST.setStringParameter('contentType', 'application/x-www-form-urlencoded');

incontactREST.setStringParameter('startDate', datestart);

incontactREST.setStringParameter('endDate', dateend);

incontactREST.setStringParameter('mediaTypeId', '0');

incontactREST.setStringParameter('outboundStrategy', '0');

incontactREST.setQueryParameter("updatedSince", updatedSince);

However, this also results in the same URL confusingly enough:

services/v11.0/agents/2471149/statehistory?mediaTypeId=0&outboundStrategy=0&endDate=2018-02-14T00:00:00-0600&updatedSince=2018-02-13T11:19:33-0600&startDate=2018-02-13T00:00:00-0600

Has anyone else run into this issue?

1 ACCEPTED SOLUTION

Erik Stolberg
Tera Guru

I've had problems with using Query Parameters, so your Method #3 option would be to just build your entire endpoint in the endpoint field and use the variables within it.



services/v11.0/agents/2471149/statehistory?outboundStrategy=${outboundStragety}&endDate=${endDate}&updatedSince=${updatedSince}

View solution in original post

2 REPLIES 2

Erik Stolberg
Tera Guru

I've had problems with using Query Parameters, so your Method #3 option would be to just build your entire endpoint in the endpoint field and use the variables within it.



services/v11.0/agents/2471149/statehistory?outboundStrategy=${outboundStragety}&endDate=${endDate}&updatedSince=${updatedSince}

Ah great idea! Through the following function:



incontactREST.setEndpont( desiredURL);