1

I am trying to debug a POST request which returns 400 using requests library.

The request is of type:

POST https://somesite.com/path?key=value
{
  "some": "payload"
}

So it has both query parameters and body.

Inspecting the response.request object I can see that the body property is null so the problem could be I am passing the arguments in the wrong way, but I couldn't figure out where the issue is.

import requests
import json


session = requests.Session()

response = session.request(
    url='https://somesite.com/path?key=value',
    method='POST',
    headers={
        'Content-Type': 'application/json'
    },
    json={
        'some': 'payload',
    }
)

print(response.status_code)  # 400
print(response.request.body)  # null

Following this discussion, I've also tried to extract the query parameters from the url and pass them using params in the request, but still no luck (400 and response.request.body=null):

import requests
import json


session = requests.Session()

response = session.request(
    url='https://somesite.com/path',
    method='POST',
    headers={
        'Content-Type': 'application/json'
    },
    params={'key': 'value'},
    json={
        'some': 'payload',
    }
)

print(response.status_code)  # 400
print(response.request.body)  # null

What am I doing wrong?

revy
  • 3,945
  • 7
  • 40
  • 85

0 Answers0