首页 > 解决方案 > 收到内容编码的响应:gzip,但未能对其进行解码

问题描述

当发出 GET 请求时

https://apicache.vudu.com/api2/?_type=contentSearch&contentEncoding=gzip&contentId={}&dimensionality=any&followup=ultraVioletability&followup=longCredits&followup=superType&followup=episodeNumberInSeason&followup=advertContentDefinitions&followup=tag&followup=hasBonusWithTagExtras&followup=subtitleTrack&followup=ratingsSummaries&followup=geneGenres&followup=seasonNumber&followup=trailerEditionId&followup=genres&followup=usefulStreamableOffers&followup=walmartOffers&followup=preOrderOffers&followup=editions&followup=merchandiseContentMaps&followup=promoTags&followup=advertEnabled&format=application%2Fjson

从 AWS,我间歇性地收到以下错误:

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

例子:

requests.get(url.format('832504'))
<Response [200]>
requests.get(url.format('460398'))
<Response [200]>
requests.get(url.format('27616'))
<Response [200]>
requests.get(url.format('23657'))
<Response [200]>
requests.get(url.format('8661'))
ContentDecodingError...
requests.get(url.format('14250'))
<Response [200]>
requests.get(url.format('516307'))
ContentDecodingError...
requests.get(url.format('10366'))
<Response [200]>

我尝试了不同的标头组合(包括此处的建议:https ://github.com/requests/requests/issues/3849 )并删除了contentEncoding=gzip参数(返回 502 状态代码)。

考虑到这个 API 在我的 PC 上完美运行,也许 Vudu 和 AWS 之间存在一些冲突?

标签: pythonpython-requests

解决方案


我认为这是当前的 python 请求库限制(或设计行为,请参见此处

所以你必须使用resp.raw来获取原始字节。(:要访问resp.raw,也需要stream=True)。

resp = session.get(url, stream=True)
data = resp.raw.read()

推荐阅读