【发布时间】:2017-03-01 12:42:38
【问题描述】:
当我连接到我的 Web API 时遇到问题,因为我无法获得结果的正文。
这是我的代码:
import json,requests
url =('URL')
data={"Content-Type":"application/x-wwwform-urlencoded", "Authorization":"Valid JWT Token"}
myResponse1 = requests.get(url,data=data)
print ("status_code:"+ str(myResponse1.status_code))
print ("******************")
print ("text:"+ str(myResponse1.text))
print ("******************")
print ("encoding:"+ str(myResponse1.encoding))
print ("******************")
print ("json:"+ str(myResponse1.json))
print ("******************")
print ("content:"+ str(myResponse1.content))
print ("******************")
print ("body:"+ str(myResponse1.body))
这个输出:
status_code:401
******************
text:
******************
encoding:None
******************
json:<bound method Response.json of <Response [401]>>
******************
content:b''
******************
Traceback (most recent call last):
File "C:\Users\Carlo\Desktop\Web API\Log-In_API.py", line 28, in <module>
print ("body:"+ str(myResponse1.body))
AttributeError: 'Response' object has no attribute 'body'
现在我不明白为什么,因为当我尝试使用 Postman 的 API 时,会返回一个正文结果:
如果您在我的结果和图片中看到状态,我们有不同的状态,但我真的不明白为什么看到我正在传递相同的参数
【问题讨论】:
-
答案实际上在您提供的屏幕截图中。 Postman 显示 headers 部分,但代码不使用任何自定义 headers,并将相关 dict 提供给 data 参数。我猜,对于 GET 请求,哪个进入 url。
-
大家好,请问为什么我在这个问题上得了负分?我正在努力改善我所有的问题,但我真的不明白为什么我在那个问题上得分为负:S
标签: python json python-3.x asp.net-web-api python-requests