# Requests.post() vs. system.net.httpPost behavior

**URL:** <https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692>\
**Category:** Ignition\
**Created:** [October 23, 2024, 8:39pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692 "2024-10-23T20:39:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [October 23, 2024, 8:39pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/1 "2024-10-23T20:39:49Z")

</div>

I was hoping someone here could give me some advice on sending a json payload to a remote endpoint.

To get the json formatted and test things out more easily in a comfortable development environment, I've been using requests.post(payload) in my code to transfer the payload to the remote site. I've been successful there, 200 for a response. When I lift that code and replace the requests.post with system.net.httpPost(payload) I'm getting 415s back, unsupported media type. Since the payload is an identical string, I assume that the Ignition function behaves differently than requests. I'm pretty sure the problem is in the parameters I'm feeding httpPost since the same payload works with requests.post(). Here's the code in question:

```python
payload = json.dumps(payload)

url = "https://ICantShowYouTheURL.com"

headerData = {"Content-Type" : "application/json"}

response = system.net.httpPost(url, headerData, payload) 
print(response)

```

Can someone shed some light on this? They're not logging the body on the receiving end so I can't tell what Ignition is actually sending.

thanks!

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [October 24, 2024, 2:47pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/2 "2024-10-24T14:47:41Z")

</div>

That is strange... what if you try something like this instead (replacing `.httpPost` with `.httpClient.post()` and not encoding `payload` as a JSON string):

```python
# Create the JythonHttpClient.
client = system.net.httpClient()
payload = payload # coming from your script above
url = "https://ICantShowYouTheURL.com"
 
# Send a POST request
### payload being a dict will automatically 
### set the content type to `application/json`
response = client.post(url, data=payload)
 
# Validate the response.
if response.good:
    # Do something with the response
    print response.json['args']['a']

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 24, 2024, 4:09pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/3 "2024-10-24T16:09:40Z")

</div>

From [system.net.httpClient | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-net/system-net-httpClient)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/1/21e97b0ab66bb8bade2075228e0de58ab2ceb6c1.png)

Exactly what @flexware said.

Also, if you do need to encode/decode json use

> **[system.util.jsonEncode | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-jsonEncode)**
>
> Takes a Python object such as a list or dictionary and converts into a JSON string.

or

> **[system.util.jsonDecode | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-jsonDecode)**
>
> Takes a JSON string and converts it into a Python object such as a list or a dictionary.

Always prefer Ignition first party java libraries, or standard java libraries, over jython libraries as a rule.

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [October 24, 2024, 8:37pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/4 "2024-10-24T20:37:56Z")

</div>

This worked. Thanks very much!

---

<div class="post-metadata">

**Author:** ![flexware](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/flexware/32/66243_2.png) [@flexware](https://forum.inductiveautomation.com/u/flexware)\
**Post date:** [October 24, 2024, 9:45pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/5 "2024-10-24T21:45:34Z")

</div>

Awesome, that's great to hear!

If you can, please mark this thread as answered so that we get that handy little icon next to the post title when browsing the forums.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/5/758ca9a3984856ed7e7b9a5f821d30492c86e97d.png)

[This helps people who are answering questions quickly see which posts need attention and which posts have been answered without clicking into them]

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [October 25, 2024, 5:03pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/6 "2024-10-25T17:03:45Z")

</div>

It turns out that it didn't quite do it. The documentation and [bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi) above indicate that a dictionary would be automatically converted to JSON with the "data = _my dictionary_" parameter but it didn't didn't do that. The endpoint accepted the data but the system behind it rejected it as malformed.

I encoded it above the client.post() with system.util.jsonEncode (thanks [bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)!) and specified the content type, and I was back in business.

thanks both of you for your help!

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 25, 2024, 5:25pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/7 "2024-10-25T17:25:22Z")

</div>

Interesting, can you show your code? I would imagine Ignition is doing a system.util.jsonEncode under the hood if it detects a dictionary, so I find it odd it only works if you manually encode it.

---

<div class="post-metadata">

**Author:** ![christopher.berg](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@christopher.berg](https://forum.inductiveautomation.com/u/christopher.berg)\
**Post date:** [October 30, 2024, 12:03pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/8 "2024-10-30T12:03:00Z")

</div>

Sorry, I just saw your request.

```python
try:
	sensorData = system.util.jsonEncode(sensorData)	
	url = "a URL I can't show you"
	client = system.net.httpClient()
	
	response = client.post(url, data = sensorData, headers = {"Content-Type":"application/json"})

	print(response)
	if response.good:
	    print("payload posted")

```

sensorData is a dictionary until the system.util.jsonEncode. Python reports that it's unicode after that. Specifying the Content-Type formatted it in an acceptable way for the remote system to process.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [October 30, 2024, 12:11pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/9 "2024-10-30T12:11:30Z")

</div>

So you are saying that what you posted works, but

```python
try:
	sensorData = {'someKey':'somevalue'}	
	url = "a URL I can't show you"
	client = system.net.httpClient()
	
	response = client.post(url, data = sensorData, headers = {"Content-Type":"application/json"})

	print(response)
	if response.good:
	    print("payload posted")

```

is throwing an error?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 30, 2024, 12:36pm UTC](https://forum.inductiveautomation.com/t/requests-post-vs-system-net-httppost-behavior/97692/10 "2024-10-30T12:36:47Z")

</div>

_Pssst!_

Don't make a new instance of `httpClient()` with every operation. It is intended to be reused over many requests. Delegate to a project library script function, and place the `client = system.net.httpClient()` call **outside** the function.
