cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

How do I structure a query parameter for a REST Call?

kbetances
2-Explorer

How do I structure a query parameter for a REST Call?

I have a data table that I would like to call the QueryDataTableEntries service on through a REST API call.

I have tried the following on my browser with nothing being returned:

http://<MyServer>/Thingworx/Things/<MyDataTable>/Services/QueryDataTableEntries?userid=<userId>&password=<password>&query={filters:{type:LIKE, fieldName:<MyFieldName>, value:<MyValue>}}

Any suggestions or tips?

Thanks

ACCEPTED SOLUTION

Accepted Solutions
posipova
20-Turquoise
(To:kbetances)

You'd have to use URL encoding to pass the json, so for example if i do a timestamp query on my QueryDataTableEntries service, my json looks like this

{"filters":{"type":"AND","filters":[{"fieldName":"timestamp","type":"BETWEEN","to":1435581955257,"from":1435553155257}]}}

Then I use this tool  https://www.url-encode-decode.com/

to get this %7B%22filters%22%3A%7B%22type%22%3A%22AND%22%2C%22filters%22%3A%5B%7B%22fieldName%22%3A%22timestamp%22%2C%22type%22%3A%22BETWEEN%22%2C%22to%22%3A1435581955257%2C%22from%22%3A1435553155257%7D%5D%7D%7D

Then my query (note this is missing the auth because i'm already authorized in the same browser session)

http://localhost/Thingworx/Things/DT/Services/QueryDataTableEntries?method=POST&content-type=application/JSON&query=%7B%…

View solution in original post

10 REPLIES 10
posipova
20-Turquoise
(To:kbetances)

Please refer to the following REST API Overview and Examples

You may also find helpful to utilize your browser dev tools in which you can see the structure of every request (f12 in chrome)

posipova
20-Turquoise
(To:posipova)

In addition, you may use Postman:

*Im planning to run this REST request in Objective-C for a native mobile app, so I am not sure how I could translate the syntax from a web tool or Postman as just a string/url.

posipova
20-Turquoise
(To:kbetances)

You would be passing it as a string/url; web tool is just for your reference on the call construction.

Right, but whats the string  structure for the query section exactly? Not sure how i will go about finding certain records that match certain values for instance.

posipova
20-Turquoise
(To:kbetances)

You'd have to use URL encoding to pass the json, so for example if i do a timestamp query on my QueryDataTableEntries service, my json looks like this

{"filters":{"type":"AND","filters":[{"fieldName":"timestamp","type":"BETWEEN","to":1435581955257,"from":1435553155257}]}}

Then I use this tool  https://www.url-encode-decode.com/

to get this %7B%22filters%22%3A%7B%22type%22%3A%22AND%22%2C%22filters%22%3A%5B%7B%22fieldName%22%3A%22timestamp%22%2C%22type%22%3A%22BETWEEN%22%2C%22to%22%3A1435581955257%2C%22from%22%3A1435553155257%7D%5D%7D%7D

Then my query (note this is missing the auth because i'm already authorized in the same browser session)

http://localhost/Thingworx/Things/DT/Services/QueryDataTableEntries?method=POST&content-type=application/JSON&query=%7B%…

Thanks a lot! This worked. Since I am using objective-c, I used:

NSString *unescaped = @"<json query test>";

NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

and appended the escapeString to the rest of my url.

posipova
20-Turquoise
(To:kbetances)

Great to hear! Sorry it took a while for me to provide what you actually asked for -- that's due to my initial misunderstanding of your need.

No worries! I could've definitely phrased my question better.

Do you perhaps know where I can find a guide to construct more complex queries. For example some queries with multiple OR's and AND's and what the json for that will look like?

I appreciate it.

posipova
20-Turquoise
(To:kbetances)

This is not quite my area of expertise, but I would say, this is a good place to start https://www.w3schools.com/js/js_json_intro.asp

Announcements


Top Tags