# Urlencoded sending a JSON string

**URL:** <https://discuss.python.org/t/urlencoded-sending-a-json-string/13795>\
**Category:** Python Help\
**Tags:** c-api\
**Created:** [February 18, 2022, 11:21pm UTC](https://discuss.python.org/t/urlencoded-sending-a-json-string/13795 "2022-02-18T23:21:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikasreddy](https://avatars.discourse-cdn.com/v4/letter/v/ad7895/32.png) [@vikasreddy](https://discuss.python.org/u/vikasreddy)\
**Post date:** [February 18, 2022, 11:21pm UTC](https://discuss.python.org/t/urlencoded-sending-a-json-string/13795/1 "2022-02-18T23:21:38Z")

</div>

I have an API that has been set up on the server side and the unit side, I have the below code in python to communicate with the API. I am now writing this for the Arduino IDE.

This code successfully communicates with the API to get the token.

How is this code sending a JSON string ‘auth\_info’ while the header reads x-www-form-urlencoded.

Even a half explanation of the following would be really appreciated.

PYTHON CODE STARTS

```auto
def get_user_auth_token():
    global token

    purl = host+'/api/oauth/token'
    head = {'Content-Type' : 'application/x-www-form-urlencoded'}    
    auth_info = {'client_id' : "unit:"+api_params.id,
                'grant_type' : 'client_credentials',
                'client_secret' : api_params.secret,
                'scope' : 'unit',
                }  

    # print(purl)
    # print(auth_info)
    # print(head)

   
    r = requests.post(purl, auth_info, head)
 
    response=json.loads(r.text)
    token = response['access_token']

```

PYTHON CODE ENDS

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [February 18, 2022, 11:55pm UTC](https://discuss.python.org/t/urlencoded-sending-a-json-string/13795/2 "2022-02-18T23:55:38Z")

</div>

> [@vikasreddy](#):
>
> How is this code sending a JSON string ‘auth\_info’ while the header reads x-www-form-urlencoded

Why do you think it’s sending JSON? It should be form encoded.

Here I run something very similar on my machine:

```python
>>> import requests
>>> head = {'Content-Type' : 'application/x-www-form-urlencoded'}
>>> auth_info = {'client_id': 'unit:5555', 'scope': 'unit', 'client_secret': 'shh.. secret!'}
>>> r = requests.post('http://localhost:8888/', auth_info, head)

```

And I just receive it in a terminal running `nc -l 8888`

```plaintext
$ nc -l 8888
POST / HTTP/1.1
Host: localhost:8888
User-Agent: python-requests/2.26.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 62
Content-Type: application/x-www-form-urlencoded

client_id=unit%3A5555&scope=unit&client_secret=shh..+secret%21

```

I don’t see any JSON being sent. This is form encoding as expected from the `post()` default.
