Home > Net >  error 400 while running PUT request in python
error 400 while running PUT request in python

Time:05-20

I have a working curl command:

ip_address='<my ip address>'
sessionID='<got from post request metadata>'

curl --insecure --request PUT 'https://'$ip_address'/eds/api/context/records' -H 'Csrf-Token: nocheck' -H 'AuthToken:<token>' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"sessionId": "'$session_ID'", "replace":false}'

The python script looks like below:

headers_put = {
    'Csrf-Token': 'nocheck',
    'AuthToken': '<my token here>',
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}

url_put = "https://"   ip_address   "/eds/api/context/records"

data = { 
    "sessionId":"<session id got from POST request metadata>", 
    "replace":"false" 
}

response = requests.put(url_put, headers=headers_put, data=data, verify=False)
print(response)

Error message I get is:

<Response [400]>
b'Bad Request: Invalid Json'

Any idea on what I am doing wrong here? Any help is appreciated!

EDIT:

Can this error cause because of data:

print(data)

{'sessionId': '<session id received from post metadata>', 'replace': 'false'}

CodePudding user response:

Http status 400 means ‘bad request’ as outlined here. You have omitted 1 request header (Csrf-token) in pyScript that was contained in your curl statement. Consider including that and retry the script.

If you still receive an error - you could try and extract (in your script) the actual text or.body of the 4xx response. It may be accessible from the text property of the python response object (you can confirm content-type) using response.apparent_encoding. Best wishes.

CodePudding user response:

I found the solution:

response = requests.put(url_put, headers=headers_put, json=data, verify=False)

instead of data= we have to use json=

  • Related