我有一个小的 python 脚本,它能够使用以下代码正确访问我的 API
import requests
auth = requests.auth.HTTPDigestAuth(user, password)
furl="http://10.101.0.203:8080/imcrs/plat/res/device"
r = requests.get(f_url, auth=auth )
在这种情况下,auth 对象可以完美运行(它能够发送 GER 请求并且 auth 对象能够成功进行身份验证,并且我得到了带有 HTTP 200 响应的预期内容)
看PAW生成的python代码
def send_request():
# My API
# GET http://10.101.0.203:8080/imcrs/plat/res/device
try:
response = requests.get(
url="http://10.101.0.203:8080/imcrs/plat/res/device",
params={
"resPrivilegeFilter": "false",
"start": "0",
"size": "10",
"orderBy": "id",
"desc": "false",
"total": "false",
},
headers={
"AuthorizationBasic Og==": "Basic YWRtaW46YWRtaW4=",
"Cookie": "JSESSIONID=D1B20BF4BB2FC9F458C8A45821FE0BDB",
},
)
print('Response HTTP Status Code: {status_code}'.format(
status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(
content=response.content))
except requests.exceptions.RequestException:
print('HTTP Request failed')
看来 PAW 正在对身份验证字符串进行编码,然后在标头中发送,这会导致立即出现 401 错误。
标头是否与上面创建的 auth 对象相同?试图弄清楚为什么这不起作用。