Discussion:
How to parse http response returned by REST call
(too old to reply)
vikash
2020-06-02 11:51:48 UTC
Permalink
Hello,

I am using ISIM REST api to perform password reset for ISIM user.

I am using http client connector and able to get JSession, LTPA token and CSRF token successfully.

next i am making a GET request to http://ISIM_HOST/itim/rest/people?uid=uid
to get person href.
I have specified request/response http headers (http.Accept,http.Content-Type,http.body.Content-Type) value as "application/json" to get the response (http.bodyAsString) in JSON format.

When i use JSON parser with http client connector i gets error (Exception occurred: java.lang.ClassCastException: java.util.ArrayList incompatible with com.ibm.di.entry.Entry), whereas I am getting the response when i am not using any parser or using HTTP Parser, but i am not able to parse this http response.
i need to get the href value from the below http.bodyAsString response

[{"_links":{"formTemplate":{"href":"/itim/rest/forms?requestee=/itim/rest/people/ZXJnbG9iYWxpZD0xNzI4MjMyOTEyMTkzMzA2MDM1LG91PTAsb3U9cGVvcGxlLGVyZ2xvYmFsaWQ9MDAwMDAwMDAwMDAwMDAwMDAwMDAsb3U7T25jb4IsZGM9Y29t&filterId=formSearch"},"self":{"href":"/itim/rest/people/ZXJnbG9iYWxpZD0xNzI4MjMyOTEyMTkzMzA2MDM1LG91PTAsb3U9cGVvcGxlLGVyZ2xvYmFsaWQ9MDAwMDAwMDAwMDAwMDAwMDAwMDAsb3U9T25jb3IsZGM9Y29t","title":"test, user"}}}]'


i tried something like this.

json = work.getString("http.bodyAsString");
jsEntry = work.fromJSON(json); // this gives *null*
href = jsEntry._links.formTemplate.href;

Could anyone please let me know where i went wrong. Thanks in advance!
Eddie Hartman
2020-06-03 20:37:38 UTC
Permalink
Post by vikash
Hello,
I am using ISIM REST api to perform password reset for ISIM user.
I am using http client connector and able to get JSession, LTPA token and CSRF token successfully.
next i am making a GET request to http://ISIM_HOST/itim/rest/people?uid=uid
to get person href.
I have specified request/response http headers (http.Accept,http.Content-Type,http.body.Content-Type) value as "application/json" to get the response (http.bodyAsString) in JSON format.
When i use JSON parser with http client connector i gets error (Exception occurred: java.lang.ClassCastException: java.util.ArrayList incompatible with com.ibm.di.entry.Entry), whereas I am getting the response when i am not using any parser or using HTTP Parser, but i am not able to parse this http response.
i need to get the href value from the below http.bodyAsString response
[{"_links":{"formTemplate":{"href":"/itim/rest/forms?requestee=/itim/rest/people/ZXJnbG9iYWxpZD0xNzI4MjMyOTEyMTkzMzA2MDM1LG91PTAsb3U9cGVvcGxlLGVyZ2xvYmFsaWQ9MDAwMDAwMDAwMDAwMDAwMDAwMDAsb3U7T25jb4IsZGM9Y29t&filterId=formSearch"},"self":{"href":"/itim/rest/people/ZXJnbG9iYWxpZD0xNzI4MjMyOTEyMTkzMzA2MDM1LG91PTAsb3U9cGVvcGxlLGVyZ2xvYmFsaWQ9MDAwMDAwMDAwMDAwMDAwMDAwMDAsb3U9T25jb3IsZGM9Y29t","title":"test, user"}}}]'
i tried something like this.
json = work.getString("http.bodyAsString");
jsEntry = work.fromJSON(json); // this gives *null*
href = jsEntry._links.formTemplate.href;
Could anyone please let me know where i went wrong. Thanks in advance!
Have you tried just parsing to a JS object:

json = work.getString("http.bodyAsString");
obj = fromJson(json);

You might want to wrap that in a try catch, in case the service does not return JSON in all situations.

try {
obj = fromJson(json);
} catch (ex) {
task.logmsg("ERROR", "Error parsing json return: " + json + " - " + ex);
}

Loading...