How to query data using GlideHTTPRequest?

Ravish Shetty
Tera Guru

Hello,

With GlideHTTPRequest, i can delete, update and insert data.

However there is this one use case where i want to fetch some data by querying it. In the documentation i did not find anything for that.

Here is the code what i use today using john andersens RESTMessageScripted. I want to use GlideHTTPRequest instead of RESTMessageScripted.


var r = new RESTMessageScripted("post", "https://"+instanceName+".service-now.com/sys_user_grmember.do");

r.addHeader("Content-Type", "application/json");

r.addRequestParameter("JSONv2", "true");

r.addRequestParameter("sysparm_query", 'group='+group+'^user='+user);

r.setBasicAuth(authenticatingUsername, authenticatingPassword);

response = r.execute();

var jsonData = new JSON().decode(response.getBody().toString());

if(jsonData.records.length == 0){

return true;

} else {

return false;

}

Edit: As shown in Line 4 of the script, i am applying a specific query. I want to do the same with GlideHTTPRequest so that i do not fetch all the data. Can i apply a specific query using GlideHTTPRequest?

1 ACCEPTED SOLUTION

Better yet, use %5E instead of ^ and it will work.



You can also use request.addParameter('sysparm_query', 'active=true%5Enumber=xxxxxxx') to set the query.


View solution in original post

16 REPLIES 16

getting an error as sn_ws is undefined. we are on eureka so i guess this won't work.


bernyalvarado
Mega Sage

Hi Ravio, the following thread may be helpful to retrieve data using GlideHTTPRequest:



Re: Incident REST API script



Thanks,


Berny


Hello Berny,



How do i specify a specific query in the url?


I tried the below using the get method but it didn't work



var request = new GlideHTTPRequest("https://XXXX.service-now.com/api/now/table/incident?&sysparm_query=active=true");  





Thanks,


Ravish


Hi Ravio,



You almost got it. Try this:



var request = new GlideHTTPRequest("https://XXXX.service-now.com/api/now/table/incident?sysparm_query=active=true");



You can add additional parameters by add ^parameter_name=parameter_value at the end of the above query. I believe this will always return to you the first records that meets that criteria.




I hope you had find this helpful!




Thanks,


Berny


This is that i trioed and this is what i got



var request = new GlideHTTPRequest('https://XXXXXXXXXXXXX.service-now.com/api/now/table/incident?sysparm_query=active=true^number=XXXXXXXXXXXXX');


request.setBasicAuth("XXXXXXXXXXXXX", "XXXXXXXXXXXXX");


request.addHeader('Accept', 'application/json');



var response = request.get();



gs.log(response.getStatusCode());


gs.log(response.getBody());



Output:




Evaluator: java.lang.IllegalArgumentException: Invalid uri 'https://XXXXXXXXXXXXX.service-now.com/api/now/table/incident?sysparm_query=active=true^number=XXXXXXXXXXXXX': Invalid query


  Caused by error in script at line 5



  2: request.setBasicAuth("XXXXXXXXXXXXX","XXXXXXXXXXXXX");


  3: request.addHeader('Accept','application/json');


  4:


==> 5: var response = request.get();


  6:


  7: gs.log(response.getStatusCode());


  8: gs.log(response.getBody());



org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)


org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)


com.glide.communications.HTTPRequest.get(HTTPRequest.java:51)


sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


java.lang.reflect.Method.invoke(Method.java:597)


org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:239)


org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1201)


org.mozilla.javascript.gen.c2294.call(<refname>:5)


org.mozilla.javascript.gen.c2294.exec(<refname>)


com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:162)


com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:176)


com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:68)


com.glide.script.Evaluator.evaluateString(Evaluator.java:161)


com.glide.script.Evaluator.evaluateString(Evaluator.java:156)


com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:282)


com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:201)


com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:159)


com.glide.processors.Processor.runProcessor(Processor.java:358)


com.glide.processors.Processor.processTransaction(Processor.java:163)


com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:146)


com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:36)


com.glide.ui.GlideServlet$1.run(GlideServlet.java:408)


java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)


java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)


java.lang.Thread.run(Thread.java:682)