Python request.post给出500响应

时间:2017-06-06 14:16:01

标签: python json rest

对于我的一个帖子请求,每次尝试运行时都会收到500响应。当我在邮递员中复制并粘贴细节时,每次都可以正常工作。 python post适用于我运行的其他请求,但是这个请求每次都失败,我无法解决原因。

之前有没有人遇到过这个问题,或者有人能看到我做错的事情吗?

json_body = '{"overrides": [], "id": 0, "name": "Rate Sheet 12", "resellerId": 2000001, "currency": "ZAR", "markup": {"id": 0, "method": "Percentage", "operand": 3}, "totalLinkedBands": 0, "totalLinkedAccounts": 0}'
token = 'JWT eyJ0eXA...s'
url = 'http://app01:8084//PriceScheme/CreatePriceScheme'
r = requests.post(url, json.loads(json_body), headers={'Authorization': token})

在Postman中,它看起来如下:

(POST) http://app01:8084//PriceScheme/CreatePriceScheme
Content-Type - application/json
Authorization - JWT eyJ...

{"overrides": [], "name": "Rate Sheet 1", "currency": "ZAR", "totalLinkedAccounts": 0, "totalLinkedBands": 1, "id": 1, "markup": {"method": "Percentage", "operand": 3.0, "id": 0}, "resellerId": "2009340"}

2 个答案:

答案 0 :(得分:0)

尝试吹嘘

requests.post(url, json = json_body, headers={'Authorization': token})

在Postman中,自动使用Content-Type - application / json

如果使用请求发布json数据,应使用json = data

答案 1 :(得分:0)

json_body='{"parametername":Value}' resp = requests.post(URL, json_body, auth=('username', 'Pass'))

解决了我的情况