gpt4 book ai didi

python 请求 POST 基本身份验证返回 200,正文为空

转载 作者:可可西里 更新时间:2023-11-01 17:34:27 26 4
gpt4 key购买 nike

import requests
s = requests.Session()
r = requests.Request('POST', 'https://'+url+'?name=value')
prep = r.prepare()
prep.headers = {'User-Agent': 'curl/7.38.0', 'Accept': '*/*', 'Authorization': 'Basic <load of hex>==', 'Content-Type': 'application/json'}
response = s.send(prep)

输出:

DEBUG:requests.packages.urllib3.connectionpool:"POST /url?name=value HTTP/1.1" 200 None

为什么我收到 200 指示身份验证成功但没有返回 json 给我必要的凭据? (如果我篡改授权 header ,它会按预期返回 403)。

我已将请求 header 直接从成功的 curl 请求获取到同一服务。为什么请求不返回任何内容?

成功的 cURL 日志:

$ curl -v https://url?name=value -X POST -H "Content-Type: application/json" -u <user:secret>
* Hostname was NOT found in DNS cache
* Trying <ip>...
* Connected to url port 443 (#0)
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
* <cert detail>
* SSL certificate verify ok.
* Server auth using Basic with user '<user>'
> POST url?name=value HTTP/1.1
> Authorization: Basic <hex encoded string>==
> User-Agent: curl/7.38.0
> Host: <host>
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 200 OK
* Server openresty/1.7.4.1 is not blacklisted
< Server: openresty/1.7.4.1
< Date: Tue, 08 Mar 2016 15:47:08 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Correlation-Id: <id>
< Strict-Transport-Security: max-age=31536000
< Cache-Control: private, no-cache, no-store, no-transform, max-age=0
< X-XSS-Protection: 1; mode=block
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
<
{"access_token": "<hex value>", "token_type": "bearer", "expires_in": "3599", "scope": "<bunch of authorisations>", "jti": "<string>"}
* Connection #0 to host <host> left intact

最佳答案

当我使用请求时,服务器返回一个空响应(即使身份验证成功并且 http 代码为 200)。

我不确定如何使用请求来避免这种情况,所以我在普通的旧 urllib 中执行了以下操作:

import urliblib.request
headers = {'User-Agent': 'blah', 'Authorization': 'Basic <hex>==', 'Accept': '*/*', 'Content-Type': 'application/json'}
req = urllib.request.Request(url, headers=headers, method='POST')
print(urllib.request.urlopen(req).readlines())

响应现在返回正确的数据。我假设服务器存在一些缓存问题,因为现在当我使用通过 urllib 添加的所有额外 header 请求时,我得到了类似的成功响应。

关于python 请求 POST 基本身份验证返回 200,正文为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com