Migrating From httpPost to httpClient...HELP!

I love my well used result = system.net.httpPost(url, params). I insert my API url and dictionary of parameters then decode the beautiful json that gets returned. Now I have to update all our legacy code and this httpClient is not making me happy! So I am getting back a 200 with the new code. The funny thing is it is not populating the $_POST variables on the server. I do not believe it is being redirected because if I put params=payload it populates $_GET. data=payload populates nothing.

below are examples of my feeble attempts.

response = system.net.httpClient.request(url=posturl, method="POST", data=payload)

response = system.net.httpClient.post(url, data=payload)

I humbly request someone tell me what I am doing wrong. Thank you.

What's in the payload variable? Is this HTTP or HTTPS?

we are using HTTP.

I think I can give you this without causing any hoopla

    def postShowSafetyAssets(self):
        window = system.vision.getWindow('Safety')
        lbl = window.rootContainer.getComponent('lblCurrentAction')
        buttons = [window.rootContainer.getComponent("btnRequest" + str(i))  for i in range(1,25)]
        posttags = ["[default]HttpRequest/Weld/urlsafety", "[default]HttpRequest/key"]
        postvals = system.tag.readBlocking(posttags)
        dict = {"k":postvals[1].value, "w":"AB", "l":"ABB Welder", "t":"welder"}
        result = system.net.httpPost(postvals[0].value, dict)
        if result!="":
            string = system.util.jsonDecode(result)
            if string['status']=="OK":
                if "items" in string:
                    message = string["subtitle"]
                    items = string["items"]
                    for item, button in zip(items, buttons):
                        button.text = item["name"]
                else:
                    pass 
                lbl.text = message

If you're using HTTP you can look at Wireshark and try to spot the difference between system.net.httpPost and system.net.httpClient.

The corporate overlords recently pwned my machine so now I can't put whatever I want on here. Could an old PHP setup be the problem? our server is only 5.3.3

I don't know a thing about PHP.

Talk to IT and get access to the tools you need to do your job.

Educated guess, try setting an explicit HTTP version of 1.1 when using httpClient.

Untested (obviously) adjusted code with no other significant changes:

def postShowSafetyAssets(self):
	window = system.vision.getWindow('Safety')
	lbl = window.rootContainer.getComponent('lblCurrentAction')
	buttons = [window.rootContainer.getComponent("btnRequest" + str(i))  for i in range(1,25)]
	posttags = ["[default]HttpRequest/Weld/urlsafety", "[default]HttpRequest/key"]
	postvals = system.tag.readBlocking(posttags)
	data = {"k":postvals[1].value, "w":"AB", "l":"ABB Welder", "t":"welder"}
	result = system.net.httpClient(version="HTTP_1_1").post(postvals[0].value, data)
	if result.good:
		if result.json['status']=="OK":
			if "items" in result.json:
				message = result.json["subtitle"]
				items = result.json["items"]
				for item, button in zip(items, buttons):
					button.text = item["name"]
			else:
				pass 
			lbl.text = message

If this works you really want to instantiate your http client in a project library script somewhere so you're not creating a whole new http client every time you click a button.


As an aside, there's zero reason for urgency in migrating away from httpPost in the first place.
When we deprecate scripting functions, we hide them to discourage new code from using them, but with extremely rare exceptions, we keep them working, with the same signature, ~forever.

Hey Paul, I thought this was "fixed" recently, in that we didn't need to worry about creating an http client within a module variable anymore, as you do it under the hood? Or did I misunderstand?

The change in 8.3 is that system.net.httpClient is auto-magically both an instance of the HTTP client (created with all default settings) and the factory function for new instances.

So you if you were previously running system.net.httpClient().doSomething(), you can swap that to system.net.httpClient.doSomething() and it'll work exactly the same, but is guaranteed to re-use the same lazily created HTTP client instance.

However, if you have to customize the creation of the http client in any way, the same don't-create-a-million-instances rules apply.

Take a very close look at the caution at the top of the docs for system.net.httpClient.

Edit: Paul was faster.

Ok, that makes sense. So the reason in this case to create as a module var is the version customisation, gotcha :+1:

I also should have checked the manual first, I didn't know it had been updated