Manuel
Manuel

Reputation: 802

Making a post request with requests library python

I know there are thousands of questions like this, but I have some doubts on how the library works when sending a post request.

From the library documentation, I can see that the parameter data should have something like A dictionary, list of tuples, bytes or a file object to send to the specified url. But I cant figure out how to put that data into the request.

Let me give an example, here is the real request to the website (Im trying to put requests into graphQL).

POST /content/v1/spaces/f8bqpb154z8p/environments/master? HTTP/1.1
Host: graphql.contentful.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: application/json
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json
Authorization: Bearer 9d5de88248563ebc0d2ad688d0473f56fcd31c600e419d6c8962f6aed0150599
Content-Length: 99


{"query":"{__schema{queryType{fields{name description}}}}","variables":null,"operationName":null}

Here is the response

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization,X-Contentful-Skip-Transformation,X-Contentful-User-Agent,X-Contentful-Enable-Alpha-Feature
Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Etag
Access-Control-Max-Age: 86400
cache-control: max-age=0
Content-Type: application/json; charset=utf-8
Contentful-Api: gql
etag: "9c9340b1cfb842f983a7c1224ed0e956"
Server: Contentful
Strict-Transport-Security: max-age=15768000
X-Content-Type-Options: nosniff
x-contentful-graphql-query-cost: 0
X-Contentful-Region: us-east-1
Content-Length: 1076
Accept-Ranges: bytes
Date: Thu, 10 Oct 2019 18:10:48 GMT
Via: 1.1 varnish
Age: 0
Connection: keep-alive
X-Served-By: cache-eze19324-EZE
X-Cache: MISS
X-Cache-Hits: 0
Vary: accept-encoding
x-contentful-request-id: 7307ef99-3c68-4381-807f-177e61c16a60

{"data":{"__schema":{"queryType":{"fields":[{"name":"asset","description":null},{"name":"assetCollection","description":null},{"name":"lesson","description":null},{"name":"lessonCollection","description":null},{"name":"lessonImage","description":null},{"name":"lessonImageCollection","description":null},{"name":"lessonCopy","description":null},{"name":"lessonCopyCollection","description":null},{"name":"layout","description":null},{"name":"layoutCollection","description":null},{"name":"lessonCodeSnippets","description":null},{"name":"lessonCodeSnippetsCollection","description":null},{"name":"course","description":null},{"name":"courseCollection","description":null},{"name":"layoutCopy","description":null},{"name":"layoutCopyCollection","description":null},{"name":"layoutHeroImage","description":null},{"name":"layoutHeroImageCollection","description":null},{"name":"layoutHighlightedCourse","description":null},{"name":"layoutHighlightedCourseCollection","description":null},{"name":"category","description":null},{"name":"categoryCollection","description":null}]}}}}

Here is what Im doing in my python code

data = {'query':'{__schema{queryType{fields{name description}}}}','variables':null,'operationName':null}
headers = {'Authorization': 'Bearer 9d5de88248563ebc0d2ad688d0473f56fcd31c600e419d6c8962f6aed0150599',
'Host': 'graphql.contentful.com'}

response = requests.post('/content/v1/spaces/f8bqpb154z8p/environments/master?', data=data, headers=headers)

Here is the response

{"errors":[{"message":"Unknown operation named \"null\"."}]}

With Burp I tried removing headers from the original request so I could see only the data I need for the succesful request and only authorization and host is needed.

However, the request I build does not return the same as the request made withing the browser.

Am I doing somehting wrong? What actually goes inside the data parameter in the post method? Maybe thats not the place I need to put my dictionary.

Thanks for the help!

Upvotes: 0

Views: 257

Answers (1)

Narcisse Doudieu Siewe
Narcisse Doudieu Siewe

Reputation: 1104

there is no 'null' in python! replace 'null' by 'None' and and try or use this

data = {'query':'{__schema{queryType{fields{name description}}}}','variables':None,'operationName':None}
headers = {'Authorization': 'Bearer 9d5de88248563ebc0d2ad688d0473f56fcd31c600e419d6c8962f6aed0150599','Host': 'graphql.contentful.com'}
response = requests.post('http://graphql.contentful.com/content/v1/spaces/f8bqpb154z8p/environments/master?', json=data, headers=headers)

Upvotes: 1

Related Questions