简体   繁体   English

请求未能解码响应

[英]Requests failed to decode response

I want to use python requests to get response from API.我想使用 python 请求从 API 获取响应。 In postman this works.在邮递员中这是有效的。 I use headers Content-Type application/json and in body raw JSON.我使用标题Content-Type application/json和正文原始 JSON。 How I can make this in python?我怎么能在python中做到这一点?

Now I get the following contentDecodingError现在我得到以下contentDecodingError

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

Example Code示例代码

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)

Your module expects gzip but the server doesn't return gzip. 您的模块需要gzip但服务器不会返回gzip。 You could try to add an Encoding-Type. 您可以尝试添加编码类型。 I cannot guarantee the type, this depends on your api. 我无法保证类型,这取决于你的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)

如果我添加encode = c('json'),这对我来说是有效的。

Were you able to solve the problem at the python level?你能在python层面解决问题吗? Because I was able to download only the basic data about the campaign, but it does not display the data to me as in php因为我只能下载关于活动的基本数据,但它不像在 php 中那样向我显示数据

import requests
import urllib3
import json
urllib3.disable_warnings()
import zlib
import gzip


    url = 'http://api.semstorm.com/api-v3/monitoring/monitoring-campaign/'
    headers = {
    'Content-Type': 'application/json',
    'Accept-Encoding': 'gzip',
}

campaign_id = xxxxx

hed = {'Content-Type': 'application/json', 'Accept-Encoding': 'deflate'}
data = {
    'services_token': 'xxxxx', 
    'campaign_id':campaign_id,
    "datemin": "20211101",
    "datemax": "20211130",
    "gap": "monthly"
    
}
url_new = url + str(campaign_id) +'.json'
from pandas import json_normalize
r= requests.get(url_new, headers=headers, json = data,   allow_redirects=False, verify=False,params=data
             )
jsonResponse = json.loads(r.text)
json_normalize(jsonResponse['results'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM