python请求.put()在urllib3时失败http.请求('PUT',…)成功。什么给予?

2024-06-12 22:18:24 发布

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

我尝试使用python请求来访问atlassianconferencerestapi。在

我已经成功地调用了一个getapi,但是当我调用PUT来更新一个conference页面时,它返回200,但是没有更新页面。在

我使用chrome::YARC来验证API是否正常工作(它确实工作正常)。在尝试调试它一段时间后,我恢复尝试使用urllib3,它工作得很好。在

我真的很想使用请求,但是我一辈子都无法在调试、Google等程序之后找到这个问题

我在运行Mac/Python3:

$ uname -a
Darwin mylaptop.local 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64
$ python3 --version
Python 3.6.1

下面是我的代码,显示了我尝试的所有三种方法(两个请求和一个urllib3):

^{pr2}$

任何帮助都会很好。在

更新1:添加请求转储

-----------START-----------
PUT http://confluence.myco.com/rest/api/content/101904815
User-Agent: python-requests/2.18.4
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
Content-Length: 141
Content-Type: application/json
Authorization: Basic <auth-token-here>==

b'{"type": "page", "version": {"number": 17}, "body": {"storage": {"representation": "storage", "value": "new body here version  version 17"}}}'

Tags: hereputversionbodystorage页面contentchrome
2条回答

requests never went back to PUT (Bug???)

您所观察到的是requests与web浏览器的行为一致:使用GET请求对http302重定向做出反应。在

From Wikipedia

The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the location field.

(...)

Many web browsers implemented this code in a manner that violated this standard, changing the request type of the new request to GET, regardless of the type employed in the original request (e.g. POST)

(...)

As a consequence, the update of RFC 2616 changes the definition to allow user agents to rewrite POST to GET.

所以这个行为和RFC2616是一致的。我认为我们不能说这两个库中哪一个的行为“更正确”。在

在请求和urllib3模块如何处理从http到https的切换时,看起来是不同的。(见上文@Kos答案)。这是我在检查调试日志时发现的。在

所以在@JonClements建议我给他发送回复垃圾后,我开始思考。在做了一些研究之后,我发现了一些神奇的运行,可以对请求和urllib3进行调试(请参见here)。在

在观察两者的差异时,我注意到,对于我的公司汇流站点,它们正从http重定向到https:

urllib3:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): confluence.myco.com
DEBUG:urllib3.connectionpool:http://confluence.myco.com:80 "PUT /rest/api/content/101906196 HTTP/1.1" 302 237
DEBUG:urllib3.util.retry:Incremented Retry for (url='http://confluence.myco.com/rest/api/content/101906196'): Retry(total=2, connect=None, read=None, redirect=None, status=None)
INFO:urllib3.poolmanager:Redirecting 
    http://confluence.myco.com/rest/api/content/101906196 -> 
    https://confluence.myco.com/rest/api/content/101906196
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): confluence.myco.com
DEBUG:urllib3.connectionpool:https://confluence.myco.com:443 "PUT /rest/api/content/101906196 HTTP/1.1" 200 None

请求尝试使用我的PUT,然后在重定向后转到GET:

^{pr2}$

请求再也回不去了

我把我最初的url从http:改为https:,一切正常。在

相关问题 更多 >