How to pass a list to data parameter in requests.patch?

Viewed 471

I'm trying to add a role to a user in Auth0 via requests in python, but I'm having problems with the data part. I've tried to send this data passing a pure list, using json parameter instead of data and other options...

requests.patch(
    url='my_url',
    data=json.dumps(["my_role_id"]),
    headers={'Authorization': 'Bearer my_token',
             'Content-Type': 'application/json'}
)

Following the docs, if I try to send the same data via cURL or in a client like Insomnia, it works.

curl --request PATCH \
--url 'https://{extension_url}/users/{user_id}/roles' \
  --header 'Authorization: Bearer {access_token}' \
  --header 'Content-Type: application/json' \
  --data '[ "{role_id}" ]'
2 Answers

The data content passed to service in the python sample is not the same as in the cURL sample. Try this (postman-echo helps to figure out what is going on):

import requests
import json
role_ids = ["my-role-id"]
role_id_array = [ "{%s}" % x for x in role_ids ]
resp = requests.patch(
    url='http://postman-echo.com/patch',
    data=json.dumps(role_id_array),
    headers={'Authorization': 'Bearer my_token',
             'Content-Type': 'application/json'}
)
print(resp.content)

I solved it! My url was wrong with the user_id incomplete and Auth0's authorization API didn't tell me that.

Related