【问题标题】:Making a GET request in R在 R 中发出 GET 请求
【发布时间】:2016-02-22 17:22:27
【问题描述】:

我一直在使用 httr 和 rcurl,但无法将以下 curl GET 请求转换为 R:

curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'

特别是,我在传递授权选项时遇到了一些麻烦,因为我在两个库中都找不到等效参数。它可能是一个自定义标题?

【问题讨论】:

    标签: r http rcurl httr


    【解决方案1】:
    httr::GET('https://this.url.api.com:334/api/endpoint', 
          accept_json(), 
          add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))
    

    另见https://github.com/hrbrmstr/curlconverter

    【讨论】:

    • 卷曲转换器真的很酷!我明天会试试这个,然后会回复你,让你知道它是否有效!
    • 抱歉,过了一段时间才回复您。这个请求奏效了!尽管 JackStat 建议的 curl 转换器使在 R 中提出请求变得轻而易举!
    【解决方案2】:

    试用新的和进一步改进的 curlconverter 包。它将接受一个 curl 请求并输出一个 httr 命令。

    #devtools::install_github("hrbrmstr/curlconverter")
    
    library(curlconverter)
    
    curlExample <- "curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer 31232187asdsadh23187' 'https://this.url.api.com:334/api/endpoint'"
    
    resp <- make_req(straighten(curlExample))
    resp
    

    【讨论】:

    • 这个库太酷了!你有什么经验?你总是得到很好的卷曲转换吗?
    • 这绝对是一个我预见会得到大量使用的包。这是相当新的,但我发现很容易弹出我的 curl 请求并获得大量可以使用的 httr 代码。
    【解决方案3】:

    我同意 sckott 的回答。我对curlconverter的维护和官方不太了解,但是为了完成httr功能我会多加几行。

    getInfoInJson <- httr::GET('https://this.url.api.com:334/api/endpoint', 
          accept_json(), 
          add_headers('Authorization' = 'Bearer 31232187asdsadh23187'))
    
     #safe the info in a json object 
     jsonInfoImg <- content(getInfoInJson, type="application/json")
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多