r-lib / httr2

Make HTTP requests and process their responses. A modern reimagining of httr.
https://httr2.r-lib.org
Other
235 stars 56 forks source link

I don't understand req_auth_basic's behavior #484

Closed frzambra closed 2 months ago

frzambra commented 2 months ago

Perhaps it's a minor mistake, but I've been trying for a while and googling, and I can't seem to get it.

When I run the first, I get the status "200 Ok," but when I append a path, I get "401 Unauthorized."

request("http://agviewer.com/api/v1") |> 
    req_auth_basic(username = usr, password = pass) |> 
    req_perform() |> 
    resp_status()
[1] 200
request("http://agviewer.com/api/v1") |> 
    req_auth_basic(username = usr, password = pass) |> 
    req_url_path_append('/device/data') |> 
    req_perform() |> 
    resp_status()
Error in `req_perform()`:
! HTTP 401 Unauthorized.
hadley commented 2 months ago

A dry run confirms that the header is being set correctly:

library(httr2)

request("http://agviewer.com/api/v1") |> 
  req_auth_basic(username = "usr", password = "pass") |> 
  req_url_path_append('/device/data') |> 
  req_dry_run(redact_headers = FALSE) 
#> GET /api/v1/device/data HTTP/1.1
#> Host: agviewer.com
#> User-Agent: httr2/1.0.1.9000 r-curl/5.2.1 libcurl/8.4.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip
#> Authorization: Basic dXNyOnBhc3M=

Created on 2024-07-02 with reprex v2.1.0

This suggests that the problem isn't your understand of httr2, but something with the API you trying to call.