rtidyversehttrhttr2

Pass vector to httr2 req_headers()


This seems a bit silly but I'm trying to figure out how to pass a vector of headers to req_headers() so a toy code example that works:

req <- request("http://example.com")
req %<>% req_headers(myHeader1 = "value1", myHeader2 = "value2)

But what I'm trying to do is something like:

headerValues <- c(myHeader1 = "value1", myHeader2 = "value2")    
req <- request("http://example.com")
req %<>% req_headers(headerValues)

I've tried headerValues as a list too but no luck.


Solution

  • The syntax used in httr2 is consistent with the rest of the "tidyeval" syntax used throughout the tidyverse. If you want to inject named parmaeters into a call, use the !!! syntax.

    headerValues <- c(myHeader1 = "value1", myHeader2 = "value2")    
    req %>% req_headers(!!!headerValues)
    

    For more details, check out the rlang guide to injection.