使用请求库python发出发布请求

时间:2019-10-10 18:02:56

标签: python request python-requests

我知道有成千上万个这样的问题,但是我对图书馆发送帖子请求时的工作方式有疑问。

从库文档中,我可以看到参数data应该类似于A dictionary, list of tuples, bytes or a file object to send to the specified url。但是我不知道如何将数据放入请求中。

让我举个例子,这是对网站的真实请求(我正在尝试将请求放入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}

这是回应

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}]}}}}

这是我在我的python代码中所做的

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)

这是回应

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

使用Burp,我尝试从原始请求中删除标头,以便仅看到成功请求所需的数据,并且仅需要authorizationhost

但是,我建立的请求返回的内容与浏览器发出的请求不同。

我做错什么了吗? post方法中的data参数实际上包含什么?也许那不是我需要放字典的地方。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

python中没有'null'!将“ null”替换为“ None”,然后尝试或使用此

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)