【问题标题】:Making a post request with requests library python使用请求库 python 发出发布请求
【发布时间】:2019-10-10 18:02:56
【问题描述】:

我知道有成千上万个这样的问题,但我对库在发送帖子请求时的工作方式有一些疑问。

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

感谢您的帮助!

【问题讨论】:

  • 您的请求返回了什么?
  • 我会编辑问题!
  • data = {...,'variables':null, ...} null 是什么?这不是 Python 的内置值。
  • 打错了,我试图重现原始请求。我正在使用'null'

标签: python request python-requests


【解决方案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)

【讨论】:

  • 成功了!我想我必须重现浏览器发出的相同请求,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多