比较python请求与cu

2024-09-30 20:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用requestsrequests_oauthlib与API接口。在

我成功地验证并访问了API的所有GET方法,但是使用POST方法得到错误500。例如:

r = oauth.post("https://api.timelyapp.com/1.0/1/clients", 
data={"client":{"name":"newclient", "color":"c697c0" }}, 
allow_redirects=False, headers={"Content-Type": "application/json"})

问题是,我用curl测试了相同的调用,并且它正常工作,这里的curl代码:

^{pr2}$

我如何在requests中更深入地挖掘,以将其调用与curl进行比较?在

更新:

另外,请注意,如果不指定内容类型:

r = oauth.post("https://api.timelyapp.com/1.0/1/clients", 
data={"client":{"name":"newclient", "color":"c697c0" }}, 
allow_redirects=True)

取而代之的是302,它重定向到站点主页,在那里我获取页面的内容。在任何情况下,都不会添加新客户端。在


Tags: 方法namehttpscomclientapidatacurl
1条回答
网友
1楼 · 发布于 2024-09-30 20:25:29

您可能需要尝试以下操作:

data=json.dumps(payload)

来自python-requestsdoc

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.

相关问题 更多 >