r - 使用 httr R 接受 gzip 编码
问题描述
我订阅了金融数据提供商 ORATS。软件工程师联系我,让我知道我的 GET() 请求超时。他说允许在我的 GET() 请求标头中进行 gzip 编码。SWE 不在 R 中编写代码,并已向我发送了一些 node.js 代码供我参考。
我认为 httr GET() 请求会自动将文件压缩为 gzip。
下面是 SWE 提供的 node.js 代码,后面是我当前的 R 代码,这些代码一直有效,直到我增加了从他们的 API 中提取的文件的大小(开始超时)。
const request = require('request');
const options = {
url: 'https://api.orats.io/data/cores/general?include=earn',
headers: {
'Authorization' : 'your authorization token',
'Accept-Encoding' : 'gzip'
},
gzip : true
};
request(options, function(err, response, body){
// Body is already uncompressed b/c the request library uncompresses it for you.
console.log(JSON.parse(body));
});
R code:
library(httr)
x = GET(url, add_headers(Authorization = token))
y = rawToChar(x$content)
我希望此代码请求 gzip 文件。谢谢你。
解决方案
将同一Accept-Encoding
行也添加到 httr GET 请求中:
library(httr)
x = GET(url, add_headers(.headers = c('Authorization'= token,
'Accept-Encoding' = 'gzip, deflate')))
请注意,httr 会自动解压缩响应。
推荐阅读
- java - 使用 doReturn 为 Spring Service 实现 JUnit 测试
- powershell - .psm1 中的函数未导出
- java - Propagation Required 和 Requires_new 的示例
- go - 结果到达时如何管理缓存服务器上的重复请求
- reactjs - Material UI:在 theme.ts 文件中引用 createMuiTheme 的其他属性?
- python - Pandas 条件聚合和非条件聚合在一起
- rust - 如何拆分、去抖动和加入流?
- python - 一张图像内的关键点匹配
- microsoft-teams - 您可以在 Teams 选项卡中使用 Microsoft Graph Toolkit
- python-3.x - 为 MagickMock 设置返回值和测试 method_calls