# system.net.httpPost() throwing error while Postman is not

**URL:** <https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815>\
**Category:** Ignition\
**Tags:** ignition80, perspective\
**Created:** [March 23, 2021, 7:08pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815 "2021-03-23T19:08:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [March 23, 2021, 7:08pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/1 "2021-03-23T19:08:38Z")

</div>

I’m having trouble using the `system.net.httpPost()` function to POST a message to an endpoint. I can use Postman and get the response I need but when I try to use `httpPost()` I get an error message `{"error": "unsupported_grant_type"}`.

I am wondering what I am doing differently in Postman vs Ignition that it doesn’t work.

Here is my script:

```auto
def getAccessToken():
	headers = {"Content-Type": "application/x-www-form-urlencoded","Accept": "*/*","Accept-Encoding": "gzip, deflate, br","Connection": "keep-alive"}
	
	body = {"grant_type": "refresh_token","refresh_token": getRefreshToken()}
	
	contentType = "application/x-www-form-urlencoded"
	
	url = getWebServicesRootUrl() + '/services/rest/auth/oauth2/v1/token'

	res = system.net.httpPost(url, 
				contentType = contentType, 
				postData = body, 
				username = getClientId(), 
				password = getClientSecret(), 
				headerValues = headers, 
				throwOnError = False
			)
	return res

```

Here is what the headers look like in Postman:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/9/c95f300e9e3c8ff2078f9817fffd7e848c4b6d57.png)

Here is what the body looks like in Postman:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/c/2c078a6fcf13388fe135c6b0bda7bccefb4e7dbe.png)

Here is the Authorization in Postman (that corresponds to username and password in `system.net.httpPost()`, at least that is what I assumed…):

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/9/391df81e5a471cc92057cd2d36beed49a39e243d.png)

Here are Postman settings:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/4/0469eae66d8ec8660581f2bbd4160c9be5d70cd9.png)

Any feedback will be greatly appreciated. I also tried using `system.net.httpClient()` but got the same response code.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 23, 2021, 7:21pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/2 "2021-03-23T19:21:26Z")

</div>

Try using `system.net.httpClient()` instead. `system.net.httpPost()` doesn’t know how to encode that dictionary you provide for the body into `x-www-form-urlencoded`. Or do that encoding to the body yourself and pass the resulting string in as the body.

edit: actually not sure if `system.net.httpClient()` will do that encoding for you either.

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [March 23, 2021, 7:28pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/3 "2021-03-23T19:28:14Z")

</div>

Hi Kevin,

Thanks for the quick response… That ended up being the issue.

Quick fix:

1. Add import statement `from urllib import urlencode`.
2. Call function `urlencode()` on body parameter.

Full function:

```auto
def getAccessToken():
	from urllib import urlencode
	headers = {"Content-Type": "application/x-www-form-urlencoded","Accept": "*/*","Accept-Encoding": "gzip, deflate, br","Connection": "keep-alive"}
	
	body = {"grant_type": "refresh_token","refresh_token": getRefreshToken()}
	
	contentType = "application/x-www-form-urlencoded"
	
	url = getWebServicesRootUrl() + '/services/rest/auth/oauth2/v1/token'

	res = system.net.httpPost(url, 
				contentType = contentType, 
				postData = urlencode(body), 
				username = getClientId(), 
				password = getClientSecret(), 
				headerValues = headers, 
				throwOnError = False
			)
	return res

```

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [March 23, 2021, 7:33pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/4 "2021-03-23T19:33:17Z")

</div>

> [@Kevin.Herron](#):
>
> edit: actually not sure if `system.net.httpClient()` will do that encoding for you either.

From my experience, it didn't. Thank you for your answer, that was the breakthrough I needed.

@Kevin.Herron I should probably open another thread, but I have one last question: I have a follow up HTTP POST that I need to make that has the Authorization type `Bearer Token`. Is there a way to use `system.net.httpPost()` to make this request? From the documentation, it isn't immediately clear that `system.net.httpClient()` would support it either, or that it would be any different than `httpPost()`.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 23, 2021, 7:50pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/5 "2021-03-23T19:50:45Z")

</div>

I think the only way to make that work would be put all the appropriate headers in yourself.

@paul-griffith that sound right?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 23, 2021, 8:08pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/6 "2021-03-23T20:08:12Z")

</div>

Yeah, that’s correct. The JDK HttpClient only supports Basic auth for now (with some vague idea of future support for other auth types).

And better support for form encoding in POST submissions is a planned feature, but no timeline on that.

---

<div class="post-metadata">

**Author:** ![MikeAllgood](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@MikeAllgood](https://forum.inductiveautomation.com/u/MikeAllgood)\
**Post date:** [May 24, 2022, 9:30pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/7 "2022-05-24T21:30:35Z")

</div>

I had to import the urlencode function of the urllib library to encode the data being passed in. The get open cases method of the API was a little more straight forward (no explicit encoded required). Hence I was able to get this working.  
Thanks Paul!

---

<div class="post-metadata">

**Author:** ![MikeAllgood](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@MikeAllgood](https://forum.inductiveautomation.com/u/MikeAllgood)\
**Post date:** [May 24, 2022, 9:32pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/8 "2022-05-24T21:32:35Z")

</div>

OOPS! Wrong thread! 🙂
