Urlencoded sending a JSON string

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 succesfully communicates with the API to get the token.

How is this code sending a JSON file 'auth_info' while the header reads x-www-form-urlencoded.

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

PYTHON CODE STARTS

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

Whilst someone on this forum may be able to help with Python code I assume that you realise that this is an Arduino forum

Your topic was MOVED to its current forum category as it is more suitable than the original

Thank you for moving it to the relevant category @UKHeliBob.

I posted it in the Arduino Forum because I was looking to communicate with the above API using Arduino IDE.

I read up on the httpClient library which communicate either as urlencoded and as a JSON string. But, this code is communicating a JSON string as a urlencoded and I have zero idea of what is happening here.

Thank you for your help.

Hi,@vikarreddy same think for me . I get access token but if it's no idea to post the token.I'm already working python project like python post method explanation using rest api.But in this arduino api post data again in server no ideas in my mind and refered some examples it's only one post method or other's to server url are using

I actually could not complete this because I had something else come up.

But the guy who wrote the python code got back to me.

He said that auth_info was not a JSON, but a python dictionary . I wrote the code, but could test it because my website was already down by the time.

But, we should be able to url encode all auth_info as variables and parameters I guess.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.