请求无法解码响应

时间:2019-06-13 08:22:36

标签: python python-requests

我想使用python请求从API获得响应。在邮递员中,这有效。我在内部原始JSON中使用标头Content-Type application/json。我怎样才能在python中做到这一点?

现在我得到以下contentDecodingError

'Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect data check')

示例代码

import requests
url = "http://api.semstorm.com/api-v3/monitoring/monitoring-keyword/get-list"
key = "xxxx"
dat = {'services_token' : key, 'campaign_id' : 43174}
hed = {'Content-Type': 'application/json'}


POST = requests.post(url, json = dat, headers = hed)

2 个答案:

答案 0 :(得分:0)

您的模块需要gzip,但服务器不会返回gzip。您可以尝试添加编码类型。我不能保证类型,这取决于您的api。

import requests
url = "http://api.semstorm.com/api-v3/monitoring/monitoring-keyword/get-list"
key = "xxxx"
dat = {'services_token' : key, 'campaign_id' : 43174}
hed = {'Content-Type': 'application/json', 'Accept-Encoding': 'deflate'}

POST = requests.post(url, json = dat, headers = hed)

答案 1 :(得分:0)

如果我添加encode = c('json'),则在R中对我有效。