【发布时间】:2023-03-19 14:20:01
【问题描述】:
这可以从命令行运行:
curl -H "Content-Type: application/json" -X POST -d '<my data>' http://my_user:my_pass@my_url
这在 python 脚本中不起作用:
res=requests.post(
'http://my_user:my_pass@my_url',
json='<my data>')
发生的情况是它访问服务器,但没有授权。 REST API 是用 Django Rest Framework 构建的,我得到了
{"detail":"Invalid username/password."}
http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/
密码中包含这些特殊字符 % ( \
我转义了 \ ,所以它是一个双反斜杠。我还尝试在字符串前面加上 r 和 4 个反斜杠。
我也尝试使用 auth=('my_user','my_pass') 使用不同的转义符。
我通过http://curl.trillworks.com/ 运行它,但仍然没有工作。
明天我要把我的密码改成简单的并测试一下。
如果这不起作用,我将放弃并在最后添加一个仅运行该 curl 命令的 bash 脚本。
【问题讨论】:
-
使用 auth 参数传递凭据。
-
当你使用
auth=...时,你传递的是什么数据以及你使用的是什么usr?
标签: python curl django-rest-framework python-requests pycurl