【问题标题】:httr request with multiple headers in rr中具有多个标头的httr请求
【发布时间】:2016-08-17 21:16:19
【问题描述】:

我正在尝试从 R 中的 httr 包发出请求:

POST(url = "https://website.com/api/waterfalls", 
       query = list(itemsPerPage="10", page="1", sortAsc="true", sortBy="priority"), 
 add_headers(c('Authorization'='Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiI1NmZhNzZiMzMxMTY5NzAwMDIwMDAwNDIifQ.CHjH9jQHy2-B68aBRijoZptCAtVLm9U_Z80f_XYaPEc'
               'Accept-Encoding' = 'gzip, deflate, sdch, br', 
               'Accept-Language' = 'en-US,en;q=0.8', 
               'User-Agent' = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 
               'Accept' = 'application/json, text/javascript, */*; q=0.01', 
               'Referer' = 'http://cnn.com', 
               'X-Requested-With' = 'XMLHttpRequest', 
               'Connection' = 'keep-alive',
               'Authority' = 'website.com')))

这不起作用。我认为问题在于 add_headers() 的语法不正确,你知道如何在 httr 的 POST() 中使用多个头吗?

【问题讨论】:

  • “不起作用”到底是什么意思?问题仅仅是您在 Bearer 令牌值之后缺少逗号吗?因为您的代码给了我一个语法错误,而不是任何类型的运行时错误。
  • 关于语法,?add_headers 建议 add_headers(a = 1, b = 2)add_headers(.headers = c(a = "1", b = "2")) 而不是 add_headers(c(a = "1", b = "2"))
  • 我认为您的示例中的语法不正确。例如,参数之一是:'Accept-Encoding' = 'gzip, deflate, sdch, br'。在这种情况下我怎么能不使用括号?

标签: r httr


【解决方案1】:

请求是这样的:

token_request <- POST(url = 'https://api.twitter.com/oauth2/token'
             , body = 'grant_type=client_credentials'
             , add_headers(.headers = c('Authorization'= 'xxxxxxxxxxxxxxxxxx'
                                        , 'Content-Type' = 'application/x-www-form-urlencoded')))

token_body <- content(token_request, as = 'parsed')

【讨论】:

    猜你喜欢
    • 2016-09-28
    • 2017-01-13
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多