curl POST 在命令行中有效,但在 Python 中无效

curl POST works in command line but not with Python

我需要运行一个curl POST来获得API令牌如下:

curl - i -v -k -X POST -d '{"authType":"LOCAL","password":"mypw","username":"admin"}' --header "Content-Type:application/json" "https://x.x.x.x:xxxx/myapi_auth_path/login"

一切运行一切顺利,我可以获得令牌。

现在我想用 Python requests:

import requests

address = "https://x.x.x.x:xxxx/myapi_auth_path/login"
    
headers = {"Content-Type": "application/json"}
data = {"authType": "LOCAL",
        "password": "mypw",
        "username": "admin"}

res = requests.post(address, data=data, headers=headers, verify=False)

我收到这个错误:

Expecting value: line 1 column 1 (char 0)

Python 代码有什么问题?

您正在使用 data=data,这意味着您的数据字典将是 form-encoded。

您可以改用 json=data(这也会为您设置 Content-Type header)

覆盖了in this section of the docs,