# Help with Web API Calls

**URL:** <https://forum.inductiveautomation.com/t/help-with-web-api-calls/59982>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [May 24, 2022, 7:58pm UTC](https://forum.inductiveautomation.com/t/help-with-web-api-calls/59982 "2022-05-24T19:58:03Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [May 24, 2022, 8:37pm UTC](https://forum.inductiveautomation.com/t/help-with-web-api-calls/59982/4 "2022-05-24T20:37:07Z")

</div>

Right, so the content-type out of Ignition is going to be `application/json` unless you jump through some hoops (for now) - see this post for some useful details:

> [@system.net.httpPost() throwing error while Postman is not](https://forum.inductiveautomation.com/t/system-net-httppost-throwing-error-while-postman-is-not/44815/3):
>
> Hi Kevin, Thanks for the quick response… That ended up being the issue. Quick fix: Add import statement from urllib import urlencode. Call function urlencode() on body parameter. Full function: 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 = "applic…

> [@MikeAllgood](#):
>
> We’ll be running this on a timed basis, once an hour, as that is the rate that the EasyRX system updates the cases. Would a class constructor be necessary in that case? I’ve never personally built and utilized classes like this in Ignition.

Here's the thing. Notice the example success response to the token request has an 'expires\_in' field:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/7/c7747c5e71150681bc2354fac85f9c154c527551.png)  
Assuming that's seconds (undocumented) and assuming it's 3600 seconds in a real response (again, undocumented 🙂) your token will last _exactly_ an hour. The _proper_ way to utilize an OAuth API is to authorize once, then continuously refresh your token indicating you're continuing to use it. What will happen if you just re-authenticate "from scratch" each time you use the API is up to the server, really.

If this is a one-off (there's only one place you will ever be retrieving this data, and you don't expect it to apply to more sites) then there's not a _huge_ advantage to the class based syntax. For clarity, I was picturing something like this:

```python
class EasyRxClient(object):
	def __init__ (self, clientId, clientSecret, baseUrl = "https://staging.easyrxortho.com/"):
		self.baseUrl = baseUrl
		self.clientId = clientId
		self.clientSecret = clientSecret
		self.client = system.net.httpClient()
		self.__refreshToken = None
		self.__accessToken = None

	def getOrUpdateAccessToken(self):
		# have some conditional logic, e.g. based on token expiry, to request a new access token/refresh the existing token here
		return __accessToken;

	def getOpenCases(self):
		token = self.getOrUpdateAccessToken()
		return self.client.get(self.baseUrl + "/webservice/get_open_cases")

```

Which has the advantage of being _much_ cleaner _at use site_ - wherever you care about this data, it's trivial to retrieve:

```python
easyRx = EasyRxClient(clientId, clientSecret)
openCases = easyRx.getOpenCases()

```

But I don't know your environment, your business needs, how comfortable you & your organization are with Python, how much this might need to change over time, etc. That's ultimately a judgement call for you to make, I just mentioned it as a syntactic possibility.

---

_[View the full topic](https://forum.inductiveautomation.com/t/help-with-web-api-calls/59982)._
