Not able to get system.net.httpPost to work correctly

Any help would be appreciated as this API is a pain.

def runAction(self):
	
	client = system.net.httpClient
	body = "{\r\n    \"USERNAME\":\"<uname>\",\r\n    \"PASSWORD\":\"<passwrd>\",\r\n    \"CLIENT_ID\":\"<Client ID>\"\r\n}"
	
	response = client.post("api.megamation.com/clm/DL/login", headerValues="application/json", bypassCertValidation=True, postData=body)
	decode = system.util.jsonDecode(response)
	
	system.perspective.print(decode)

I jsut get python errors within the designer and can't seem to get past this.

Thanks

At least part of the issue is that all of the named parameters you're passing into post are named incorrectly. Maybe you were looking at the docs for the legacy httpPost function? The docs for httpClient are here: system.net.httpClient - Ignition User Manual 8.1 - Ignition Documentation

You'd want to look under the JythonHttpClient Methods section as well.

Your body JSON is a bit of a mess too. Might be easier to just pass a dictionary object, but you've gotta fix the other problems first.

edit: and the very first line is wrong too, it should be a method call:

client = system.net.httpClient()
1 Like

Blockquote Might be easier to just pass a dictionary object, but you've gotta fix the other problems first.

How do I do that? The BODY of the POst message is supposed to be in json, which is the coding of the post message. This was taken directly from the API publisher for python.http

I figured out how this system does dictionary, so I am good there, so I am now getting a response at least.

Only problem is the error that the response isn't json? but it is?

inductiveautomation.ignition.common.script.JythonExecException Traceback (most recent call last): File "<function:runAction>", line 7, in runAction TypeError: jsonDecode(): 1st arg can't be coerced to String

I was able to get it. I will post my final code for anyone else.

	client = system.net.httpClient()
	body = {"USERNAME":"<uname>", "PASSWORD":"<pass>",  "CLIENT_ID":"<client_id>"}
	
	response = client.post("https://api.megamation.com/clm/DL/login", headers={"Content-Type":"application/json"},  data=body)
	res_body = response.getJson(response)
	self.session.custom.api_login = res_body