# urllib2.HTTPError: HTTP Error 422: Unprocessable Entity

**URL:** <https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762>\
**Category:** Ignition\
**Created:** [March 29, 2023, 10:34am UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762 "2023-03-29T10:34:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [March 29, 2023, 10:34am UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/1 "2023-03-29T10:34:41Z")

</div>

I am trying to read API.

The following python code works:

```auto
import requests
import json
url = '.....'
headers = {
                  'accept': 'application/json',
                  'SY-API-KEY': '.....',
                  'Content-Type': 'application/json',
                  }

json_data = {
    'ingress_id': '.....'
}
response = requests.post(url, headers=headers, json=json_data)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

```

I tried to migrate them to ignition script with the following script.

```auto
import system
url = '.....'
headers = {
                 'accept': 'application/json',
                 'SY-API-KEY': '.....',
                 'Content-Type': 'application/json',
                  }

json_data = {
    'ingress_id': '.....'
}

response = urllib2.Request(url, headers=headers, data=json_data)
contents = urllib2.urlopen(response).read()
data = system.util.jsonDecode(response)
print(data)

```

It showed error at the following step:  
contents = urllib2.urlopen(response).read()

My question is: How do I realize the following function in ignition?  
print(json.dumps(json.loads(response.text), sort\_keys=True, indent=4, separators=(",", ": ")))

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [March 29, 2023, 11:48am UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/2 "2023-03-29T11:48:35Z")

</div>

found the problem.

the data type should be converted to json data.

```auto
import system
url = '.....'
headers = {
                 'accept': 'application/json',
                 'SY-API-KEY': '.....',
                 'Content-Type': 'application/json',
                  }

json_data = {
    'ingress_id': '.....'
}
json_data = system.util.jsonEncode(json_data)
response = urllib2.Request(url, headers=headers, data=json_data)
contents = urllib2.urlopen(response).read()
data = system.util.jsonDecode(response)
print(data)

```

---

<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:** [March 29, 2023, 11:52am UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/3 "2023-03-29T11:52:34Z")

</div>

1. How are you using `print(data)` and that's working, that's python3 syntax, I'm surprised thatis not giving you an error

2. For HTTP requests it's better to use [system.net.httpClient - Ignition User Manual 8.0 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC80/system.net.httpClient)

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [March 29, 2023, 12:00pm UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/4 "2023-03-29T12:00:39Z")

</div>

print(data) is used in script console.  
In the perspective, it should be system.perspective.print()

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [March 29, 2023, 12:09pm UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/5 "2023-03-29T12:09:19Z")

</div>

what's the format to insert header and data?

I used this:

```auto
promise = client.getAsync(' ****', headers=headers, params=json_data)
response = promise.get()

```

The response is failed.

---

<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 29, 2023, 3:52pm UTC](https://forum.inductiveautomation.com/t/urllib2-httperror-http-error-422-unprocessable-entity/71762/6 "2023-03-29T15:52:29Z")

</div>

```python
url = '.....'
headers = {
    'SY-API-KEY': '.....',
}

json_data = {
    'ingress_id': '.....'
}

response = system.net.httpClient().get(url, headers=headers, data=json_data)
data = response.json()
print(data)

```

The params argument is for URL parameters. You have data to pass in, so you want the data argument. You also don't need to pass a content type or accept heading manually.
