HTTP get - send body text

Hi All

I've written a short script that pulls data from a web server. This works fine but I would like to send some arguments in the body of the HTTP command so that I can filter the results.

The documentation from the web server:

Body - Array of tag names. When not set returns the value of all tags in the scene.
e.g.
[
"name0",
"name1"
]

EXAMPLE REQUEST

GET /api/tag/values/by-name
Content-Type: application/json

[
"At left entry",
"Left conveyor",
"Name of non-existing tag",
"Left count",
"Weight"
]

My current script returns the data for all tags and is as follows:

#The command - see manual. Note, remove "GET"
command = "/api/tag/values/by-name"
#Build the url
url = "http://192.168.10.96:7410%s" %(command)
#Send the command
response = system.net.httpGet(url)
#Decode the response
respjson = system.util.jsonDecode(response)
#Print the response on the screen. This is an array of dict.
print respjson
#Print the first array element.
print respjson[0]

Please can someone help me to include the tag names as arguments? The manual seems to say that I need to add these in the form of a dictionary. I have tried this but all attempts have failed.

https://docs.inductiveautomation.com/display/DOC80/system.net.httpGet

Best regards.

GET requests, by definition, do not have a body - you can only pass parameters via URL parameters.
It’s a short snippet of manual, but I’m guessing you call /api/tag/values/by-name with GET and get a list of all possible values - and then you can likely PUT, PATCH, or POST new values into that list.

As an aside, for all 8.0+ HTTP interactions I highly recommend switching to system.net.httpClient(). It’s faster, more ergonomic to use from scripting, and has a bunch of builtin features that aren’t possible via system.net.httpGet/Post/etc. For instance, your call to httpGet followed by jsonDecode can be handled in one line:
response = system.net.httpClient().get(url).json
And if you instantiate the httpClient() separately and reuse it, you can take advantage of HTTP connection pooling automatically.

1 Like

Thanks and sorry for the slow reply. I used system.net.httpClient() with great results. After spending ages importing third party libraries, this is great. For anyone who is interested, my new code is as follows. This allows me to send a header and parameters. I can use it to read and write data. Thanks again.

#The command - see manual. Note, remove "GET"
command = "/api/tag/values/by-name"
#Build the url
url = "http://192.168.10.96:7410%s" %(command)
#Declare a client
client = system.net.httpClient()
#Send the request	
response = client.put(url, {'Content-Type': 'application/json',}, '[{"name":"Run","value":true}]')
#Print the response
print response.json
1 Like