使用请求库python发出post请求

2024-09-26 17:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道像这样的问题有上千个,但我对图书馆在发出发帖请求时的工作方式有些怀疑

从库文档中,我可以看到参数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参数中实际包含了什么?也许那不是我需要放字典的地方

谢谢你的帮助


Tags: namedataaccessapplicationtypedescriptioncontentgraphql
1条回答
网友
1楼 · 发布于 2024-09-26 17:41:16

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)

相关问题 更多 >

    热门问题