A
A
AlmazKayum2020-12-09 17:09:34
Python
AlmazKayum, 2020-12-09 17:09:34

PUT request works in Curl, why doesn't it work in Python3?

I try the PUT method in curl and in python, but the result is different.
We need to enable the enabled checkbox on the server.
this is how it works, code 200, enabled=True

curl --user user:pass 
-X PUT "https://api.url/27" 
-H "Accept: application/json" -H "Content-Type: application/json" -H "Token: token" 
-d "{ \"enabled\": true}"

this is how it works, code 200, but does not change the enabled=True checkbox.
headers = {
    'Accept': 'application/json',
    'Content-type': 'application/json',
    'Token': 'token'
}
auth = ('user', 'pass')

def enabled_settings(order_id):
    enabled = True
    data = {"enabled": enabled}
    res = requests.put(f'api.url/{order_id}', 
        data=data,
        headers=headers,
        auth=auth)
    return res.json()

I don't understand what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Karbivnichy, 2020-12-09
@hottabxp

Run your Curl through the curl.trillworks.com service

T
tema_sun, 2020-12-09
@tema_sun

Try like this:

...
res = requests.put(f'{api.url/{order_id}', 
        json=data,
        headers=headers,
        auth=auth)
...

S
soremix, 2020-12-09
@SoreMix

Is the line written correctly in the source code? And if so?
f'{api.url/{order_id}'

requests.put(f'{api.url/{order_id}', 
        json=data,
        headers=headers,
        auth=auth)

Either then
payload="{\"enabled\": true}"
requests.put(f'{api.url/{order_id}', 
        data=payload,
        headers=headers,
        auth=auth)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question