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

python请求POST基本身份验证返回200,正文为空,python,http,curl,https,python-requests,Python,Http,Curl,Https,Python Requests,为什么我得到200表示身份验证成功,但没有返回json给我必要的凭据?(如果我篡改了授权头,它将按预期返回403) 我直接从一个成功的curl请求到同一个服务获取了请求头。为什么请求不返回任何内容 成功的卷曲日志: DEBUG:requests.packages.urllib3.connectionpool:"POST /url?name=value HTTP/1.1" 200 None $curl-vhttps://url?name=value -X POST-H“内容类型:applicat

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

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

成功的卷曲日志:

DEBUG:requests.packages.urllib3.connectionpool:"POST /url?name=value HTTP/1.1" 200 None
$curl-vhttps://url?name=value -X POST-H“内容类型:application/json”-u
*在DNS缓存中找不到主机名
*努力。。。
*已连接到url端口443(#0)
*已成功设置证书验证位置:
*卡菲尔:没有
CApath:/etc/ssl/certs
*SSLv3,TLS握手,客户端hello(1):
*SSLv3,TLS握手,服务器hello(2):
*SSLv3,TLS握手,证书(11):
*SSLv3、TLS握手、服务器密钥交换(12):
*SSLv3,TLS握手,服务器完成(14):
*SSLv3、TLS握手、客户端密钥交换(16):
*SSLv3,TLS更改密码,客户端你好(1):
*SSLv3,TLS握手,完成(20):
*SSLv3,TLS更改密码,客户端你好(1):
*SSLv3,TLS握手,完成(20):
*使用TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384的SSL连接
*服务器证书:
*        
*SSL证书验证正常。
*对用户“”使用Basic进行服务器身份验证
>POST url?名称=值HTTP/1.1
>授权:基本==
>用户代理:curl/7.38.0
>主持人:
>接受:*/*
>内容类型:application/json
>
当我使用请求时,服务器返回一个空响应(即使身份验证成功,http代码为200)

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

$ 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
导入urliblib.request
headers={'User-Agent':'blah','Authorization':'Basic==','Accept':'*/*','Content Type':'application/json'}
req=urllib.request.request(url,headers=headers,method='POST')
打印(urllib.request.urlopen(req.readlines())

现在,响应返回正确的数据。我假设服务器存在一些缓存问题,就像现在一样,当我使用通过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())
来自请求导入会话的

s=会话()
url='abc.com'
headers={'User-Agent':'blah','Authorization':'Basic==','Accept':'*/*','Content Type':'application/json'}
#添加标题
s、 headers.update(标题)
res=s.post(url)
data=res.json()

您可以将data={'key':'value'}作为post加载,但我只能猜测,您不知道自己的url。

为什么要发布一个空请求,而将内容类型设置为
application/json
?为什么要使用
requests.Request()
而不是
requests.post(url,headers={…})
?还有对基本auth的支持(不需要自己破解标题)。你能分享你成功使用的curl命令吗?Martijn Pieters见更新。我发布了一个空请求,以完全模仿我从cURL日志中看到的内容。它将数据作为查询字符串和内容集发布到url。我以前使用过requests.post,但我尝试了session etc,以获得更大的控制权,因为我一直得到空的200秒;通过
请求,您可以大大简化代码。post('https://'+url,params={'name':'value'},data='',auth=('user','secret'),headers={'Content-Type':'application/json'})
您可以使用
http://httpbin.org/post
要查看curl和
请求之间可能存在的进一步差异,请参见,但这基本上归结为猜测服务器在哪里出了问题。
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())
from requests import Session

s = Session()

url = 'abc.com'

headers = {'User-Agent': 'blah', 'Authorization': 'Basic <hex>==', 'Accept': '*/*', 'Content-Type': 'application/json'}

# Add headers
s.headers.update(headers)

res = s.post(url)

data = res.json()