首页 > 解决方案 > 使用 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")

谢谢高塔姆

标签: r

解决方案


试图用评论中的一段来解释以下内容会很痛苦,所以看看:

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”标头。


推荐阅读