Discussion:
Query parameters in a REST API call
Add Reply
rp
2017-07-19 17:29:15 UTC
Reply
Permalink
Raw Message
Folks, I am working on developing a custom ISIM adapter that uses a rest api to talk to a backend application. the Create User calls that i am working on now expect the input parms in query string fashion. I have usually seen the parms passed in the body of the request as JSON so this is something new for me.

For eg:

https://xxxx/api/v2/CreateUser/Personnel/User?UserInternalID=xyyy&Name=Duck, Donald.

The input data /attributes will be mapped from ISIM in the IWE that gets added by the TCB from the dispatcher. Only 2 values are mandatory and there are several other attributes all of which are optional.

What is the best way to handle this sort of call? Right now am using an HTTP Client connector to PUT the request to the API and building the qs manually with only the 2 required attributes but i can see this being very painful to build for the 20 other attributes and also given the fact they are optional.

Any and all ideas are appreciated!
yn2000
2017-07-20 17:30:35 UTC
Reply
Permalink
Raw Message
I think it is legitimate enough to say that the target does not provide the normal/common way of using the JSON (not to mention SCIM) communication method for creating a user account. For example, for my curiosity only, what would you put in the body message, if all data is in the URL, and so on.

I also think that you do not have other option, beside scripting them all. (Custom method need custom scripting, so to speak) Having said, if you do the scripting right, such as an easily to change mapping attribute within the script, I don't think it will be that painful.

Rgds. YN.
rp
2017-07-20 18:27:07 UTC
Reply
Permalink
Raw Message
Right the target isn't using json at eleast from what I can tell. I have asked for interface specific documentwtion to better understand what the API methods and request /response are. So far all I have is an endpoint and this is what I have figured out with the query string parms.
yn2000
2017-07-21 17:10:27 UTC
Reply
Permalink
Raw Message
If I am in your position, I would do the same, using HTTP Client connector, no parser, and then construct the script to manipulate the URL parameter value.
Rgds. YN.
rp
2017-07-24 15:05:02 UTC
Reply
Permalink
Raw Message
Post by yn2000
If I am in your position, I would do the same, using HTTP Client connector, no parser, and then construct the script to manipulate the URL parameter value.
Rgds. YN.
I ended up writing a function to build the query string for the API call.

function buildQS() {
var qs="";
var userinternalid=work.getString("epicUserInternalID");
var name = work.getString("epicname");
var sysloginid=work.getString("epicSystemLoginID");
var enddate = work.getString("epicEndDate");
var contactdate =work.getString("epicContactDate");
var startdate = work.getString("epicStartDate");
var isactive = work.getString("epicisactive");
var isblocked = work.getString("epicblockstatusisblocked");
var inbox = work.getString("epicinbasketclassifications");

qs += "?UserInternalID="+userinternalid;

qs += "&Name="+java.net.URLEncoder.encode(name,"UTF-8");
if ( sysloginid!=null)
qs += "&SystemLoginID="+sysloginid;
if ( enddate!=null)
qs += "&EndDate="+enddate;
if (contactdate !=null)qs += "&ContactDate="+contactdate;
if(startdate !=null)qs += "&StartDate="+startdate;
if(isactive !=null)qs += "&IsActive="+isactive;
if(isblocked !=null)qs += "&IsBlocked="+isblocked;
if(inbox !=null)qs += "&InBasketClassifications="+inbox;

task.logmsg("Query parms: "+qs);
return qs;


}


And in the "before execute" Hook i build the URL and update it for the HTTP client connector:

thisConnector.setConnectorParam("url", apiUrl.toString()+addUserUrl+queryparms);


Ugly but it seems to work. Now if only the API conformed to a normal REST API Call where you pass the attributes to the call as JSON in a POST body, things would be different.
Eddie Hartman
2017-07-24 17:59:08 UTC
Reply
Permalink
Raw Message
Post by rp
Post by yn2000
If I am in your position, I would do the same, using HTTP Client connector, no parser, and then construct the script to manipulate the URL parameter value.
Rgds. YN.
I ended up writing a function to build the query string for the API call.
function buildQS() {
var qs="";
var userinternalid=work.getString("epicUserInternalID");
var name = work.getString("epicname");
var sysloginid=work.getString("epicSystemLoginID");
var enddate = work.getString("epicEndDate");
var contactdate =work.getString("epicContactDate");
var startdate = work.getString("epicStartDate");
var isactive = work.getString("epicisactive");
var isblocked = work.getString("epicblockstatusisblocked");
var inbox = work.getString("epicinbasketclassifications");
qs += "?UserInternalID="+userinternalid;
qs += "&Name="+java.net.URLEncoder.encode(name,"UTF-8");
if ( sysloginid!=null)
qs += "&SystemLoginID="+sysloginid;
if ( enddate!=null)
qs += "&EndDate="+enddate;
if (contactdate !=null)qs += "&ContactDate="+contactdate;
if(startdate !=null)qs += "&StartDate="+startdate;
if(isactive !=null)qs += "&IsActive="+isactive;
if(isblocked !=null)qs += "&IsBlocked="+isblocked;
if(inbox !=null)qs += "&InBasketClassifications="+inbox;
task.logmsg("Query parms: "+qs);
return qs;
}
thisConnector.setConnectorParam("url", apiUrl.toString()+addUserUrl+queryparms);
Ugly but it seems to work. Now if only the API conformed to a normal REST API Call where you pass the attributes to the call as JSON in a POST body, things would be different.
Ugly is sometimes what is necessary to get the job done :) You might also want to build your query string first and then make sure it gets URL encoded:

qs = java.net.URLEncoder.encode(qs);

Escape some of those pesky special characters in attribute values
rp
2017-07-25 13:30:00 UTC
Reply
Permalink
Raw Message
Thanks Eddie ! Your blogs and responses here have been super helpful in the past also !

So I am already encoding the name field which is the only one that would have the special characters if any. The rest of the attribute are going o be numeric and dates. Do you think the entire string needs to be encoded in that case ?
Eddie Hartman
2017-07-25 17:32:35 UTC
Reply
Permalink
Raw Message
Post by rp
Thanks Eddie ! Your blogs and responses here have been super helpful in the past also !
So I am already encoding the name field which is the only one that would have the special characters if any. The rest of the attribute are going o be numeric and dates. Do you think the entire string needs to be encoded in that case ?
I usually do - trying to to make any assumptions on the data itself. Can't hurt. And glad my ramblings help :)
Eddie Hartman
2017-07-27 10:50:10 UTC
Reply
Permalink
Raw Message
Post by Eddie Hartman
Post by rp
Thanks Eddie ! Your blogs and responses here have been super helpful in the past also !
So I am already encoding the name field which is the only one that would have the special characters if any. The rest of the attribute are going o be numeric and dates. Do you think the entire string needs to be encoded in that case ?
I usually do - trying to to make any assumptions on the data itself. Can't hurt. And glad my ramblings help :)
Oops - I meant that I try NOT to make assumptions. Hence my code has a lot of try-catches :P
Loading...