bravebull
bravebull

Reputation: 21

Request to API using httr2 not changing like httr

I'm trying to switch from the R package httr to httr2

httr2 is the modern rewrite and should be superior, but I'm a novice when it comes to APIs and coding and I've been stuck all day trying to figure out what I'm doing wrong. I can only get httr to work with this API.

I believe I am messing up with adding the headers, as I don't think the path sent to the API is changing. So my problem is the request gets rejected simply because the API can't read my API key.

Here is what I have done in httr2:

    gov_url <- "https://api.dummy.gov/aaa/bb/cc"

    resp <- request(gov_url) %>% 
       req_headers(
          param1  = "10", 
          api_key = "abcdefg",
          param2  = "xyz",
          param3  = "09/10/2022"
       ) %>%
     req_dry_run()

Output:

    GET /destiny/v1/placeholder HTTP/1.1
    Host: api.dummy.gov
    User-Agent: httr2/0.2.1 r-curl/4.3.2 libcurl/7.64.1
    Accept: */*
    Accept-Encoding: deflate, gzip
    param1: 10
    api_key: abcdefg
    param2: xyz
    param3: 09/10/2022

The first line of that output hasn't changed.

    GET /destiny/v1/placeholder HTTP/1.1

Showing the structure of the 'resp' object with str(resp)

    List of 7
     $ url     : chr "https://api.dummy.gov/destiny/v1/placeholder"
     $ method  : NULL
     $ headers :List of 4
      ..$ param1    : chr "10"
      ..$ api_key   : chr "abcdefg"
      ..$ param2    : chr "xyz"
      ..$ param3    : chr "09/10/2022"
     $ body    : NULL
     $ fields  : list()
     $ options : list()
     $ policies: list()
     - attr(*, "class")= chr "httr2_request"

Sending the request with resp %>% req_perform(verbosity = 2) gives me this error:

    HTTP/1.1 403 Forbidden
---
    "error": 
     "code": "API_KEY_MISSING",
       "message": "No api_key was supplied. Please submit with a valid API key."

But when I use httr though, I can pull data from the API.

    gov_url <- "https://api.dummy.gov/destiny/v1/placeholder"

    query_params <- list('param1'    = '10',
                         'api_key'   = "abcdefg",
                         'param2'    = 'xyz',
                         'param3'    = '09/10/2022')

    gov_api <- GET(path, query = query_params)

Showing structure str(gov_api) shows the path has changed which is quite good because it matches the example input provided from the API

    List of 10
     $ url        : chr "https://api.dummy.gov/destiny/v1/placeholder?param1=10&api_key=abcdefg&param2"| __trunc
     $ status_code: int 200
     $ headers    :List of 19
      ..$ xyz                     : chr "1"
      ..$ abc                     : chr "application/json"
      ..$ edg                     : chr "something" 

And http_status(gov_api) shows its making the connection

    $message
    "Success: (200) OK"

Then I'm able to successfully use httr to pull data from the API.


Thank you to anyone who has read this far. I'd appreciate any feedback if possible.

Other things I've tried to no avail:

  1. !!! to evaluate the list of expressions
  2. Sending the API a list named queue like I do in httr
  3. Sending the param "accept" = "application/json"
  4. Different syntax, quotations

Upvotes: 2

Views: 1764

Answers (1)

Paul Carteron
Paul Carteron

Reputation: 51

I'm not sure what you're after, but if you want to reproduce same comportement as httr with httr2 you can do :

library(httr2)
gov_url <- "https://api.dummy.gov/aaa/bb/cc"

param <- list(param1  = "10",
              api_key = "abcdefg",
              param2  = "xyz",
              param3  = "09/10/2022")

resp <- request(gov_url) %>% 
   req_url_query(!!!param)

resp$url
#> [1] "https://api.dummy.gov/aaa/bb/cc?param1=10&api_key=abcdefg&param2=xyz&param3=09%2F10%2F2022"

Perhaps the confusion come from the fact that in httr2 you're setting headers and in httr you're setting query.

Upvotes: 1

Related Questions