'Different results with using curl vs python requests

I'm working on a python wrapper for Nozbe (GTD / Task organizer). Per the documentation, this curl command works to create a task.

curl -i https://api.nozbe.com:3000/task?access_token=<ACCESS_TOKEN> -XPOST -d'name=some task'

The task created is "some task"

If I attempt the equivalent behavior using python requests , the task entry is created but instead of "some task" , it results in "some+task".

The initial python code looks like this:

url = 'https://api.nozbe.com:3000/task?access_token=<ACCESS_TOKEN>'
data = {"name":"some task"}
response = requests.post(url, data=data)

I thought it might be a difference in the headers, so I ran the curl command with -vvv and confirmed value passed as
Content-Type: application/x-www-form-urlencoded

if I replicate this in the python code as follows, it produces the same "some+task" entry.

url = 'https://api.nozbe.com:3000/task?access_token=<ACCESS_TOKEN>'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {"name":"some task"}
response = requests.post(url, headers=headers, data=data)

I'm at a loss for what I'm missing to consider with this.



Solution 1:[1]

I believe the x-www-form-urlencoded is encoding the data, and that's the reason you are getting a + in place of a space.

Add ContentType as JSON:

headers = {'Content-Type': 'application/json'}

And, your requests call should be

response = requests.post(url, headers=headers, data=json.dumps(data))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanil