python - 收到内容编码的响应: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 之间存在一些冲突?
解决方案
我认为这是当前的 python 请求库限制(或设计行为,请参见此处)
所以你必须使用resp.raw
来获取原始字节。(注:要访问resp.raw
,也需要stream=True
)。
resp = session.get(url, stream=True)
data = resp.raw.read()
推荐阅读
- database - mongo 查询 - 从对象数组中获取特定对象(它的 `_id` 是已知的)但是这个数组也是文档列表的一部分
- javascript - 承诺一个 redux 表单 onSubmit 函数
- ruby-on-rails - has_many 关联状态检查执行 N+1 查询活动管理员
- angular - 我们可以从 Angular 4 中的本地路径读取文件吗?
- apache-flink - 如何高效地计算来自一个来源的多 flink 流结果
- wordpress - WordPress 自动重定向到登录页面
- php - 在 Laravel 中使用通过 composer 安装的包
- graphql - 是否可以使用 VTL 随机化 DynamoDB 查询结果?
- asp.net-mvc - 从 __MigrationHistory 表中删除数据后的 EF 6 Code First 迁移
- html - 当我以角度 5 重复表单数组时,下拉项目被填充