12

I'm trying to make a request from httr package in R:

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')))

This doesn't work. I think the problem is that the syntax for add_headers() is not correct, do you know how to use multiple headers in POST() in httr?

Yos
  • 1,276
  • 1
  • 20
  • 40
  • What exactly does "doesn't work" mean? Is the problem just that you are missing the comma after your Bearer token value? Because your code gives me a syntax error, not a run time error of any sort. – MrFlick Aug 17 '16 at 21:20
  • 2
    Concerning syntax, `?add_headers` suggest `add_headers(a = 1, b = 2)` or `add_headers(.headers = c(a = "1", b = "2"))` and not `add_headers(c(a = "1", b = "2"))`. – lukeA Aug 18 '16 at 08:35
  • I don't think the syntax is correct in your example. For instance, one of the parameters is this: 'Accept-Encoding' = 'gzip, deflate, sdch, br'. How can I not use parenthesis in this case? – Yos Aug 28 '16 at 14:47

1 Answers1

13

The request would be something like this:

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')
skap
  • 493
  • 4
  • 9