r - 使用 R 在 API 中上传 CSV 文件
问题描述
有人可以帮助使用 R 连接 API 并使用 R 将包含大约 2k 条记录的文件上传到 API 中的代码吗
我尝试了以下代码但没有帮助:
apiurl <- "https://api.thomsonreuters.com/permid/match/file"
Records <- "C:\\Users\\U6028364\\Downloads\\Organization_input_template_with_examples.csv
resp <- POST(apiurl,body=list(
addressFile=upload_file(Records),
Content-Type="multipart",
))
stop_for_status(resp)
get_data <- content(resp,"text")
谢谢高塔姆
解决方案
试图用评论中的一段来解释以下内容会很痛苦,所以看看:
api_url <- "https://api.thomsonreuters.com/permid/match/file"
records <- "C:\\Users\\U6028364\\Downloads\\Organization_input_template_with_examples.csv"
httr::POST(
url = api_url,
body = list(
addressFile = httr::upload_file(records),
),
encode = "multipart"
) -> resp
httr::stop_for_status(resp)
get_data <- content(resp, "text")
唯一真正的变化是您需要使用encode
参数(另外,您在问题代码中错过了 a "
)。
我无权访问 TR API,因此无法对此进行测试。但是,https ://docs-developers.thomsonreuters.com/1544617757924/45690/wwhelp/wwhimpl/js/html/wwhelp.htm#href=PermID%20Service/PermID%20APIs%20User%20Guide.1.27.html说这是curl
命令行来做你想做的事:
curl -X POST https://api.thomsonreuters.com/permid/match/file
-H "Content-Type: multipart/form data"
-H "x-openmatch-numberOfMatchesPerRecord: 1"
-H "x-openmatch-datatype: Organization"
-H "X-AG-Access-Token: <token>"
-F file=@OrgRecords.csv
如果是这种情况,您会遗漏一些东西并且还使用了不正确的body
,所以这可能会更好:
httr::POST(
url = api_url,
httr::add_headers(
`X-AG-Access-Token` = "YOUR_ACCESS_TOKEN_WHICH_YOU_RLY_SHLDNT_PUT_DIRECTLY_IN_R_SCRIPTS"
)
body = list(
file = httr::upload_file(records)
),
encode = "multipart"
) -> resp
我不知道是否需要其他“openmatch”标头。
推荐阅读
- python - 如何使用服务帐户确定 Google BigQuery 作业的状态?
- php - Laravel 将我的数据数组显示到 index.blade.php - 未定义的偏移量 1
- javascript - 以反应的形式上传文件
- javascript - 使用 Javascript 重用与 Azure Functions 的数据库连接
- css - 选择元素文本的垂直居中
- xaml - Xamarin ControlTemplate 设置 BindingContext
- database - 使用 Join 和计算进行查询
- wordpress - 如何使用子主题更新 WordPress 主题文件
- javascript - 尝试使用 Gmail API 计算 Gmail 中使用的标签数量
- javascript - 如何使用 JS/JQuery 根据位置在另一个页面上重定向用户?