Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse multiple link headers #606

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* `resp_link_url()` now works if there are multiple `Link` headers (#587).
* New `url_modify()` makes it easier to modify an existing url (#464).
* New `req_url_relative()` for constructing relative urls (#449).
* `url_parse()` gains `base_url` argument so you can also use it to parse relative URLs (#449).
Expand Down
4 changes: 3 additions & 1 deletion R/resp-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ resp_link_url <- function(resp, rel) {
return()
}

links <- parse_link(resp_header(resp, "Link"))
headers <- resp_headers(resp)
link_headers <- headers[names(headers) == "Link"]
links <- unlist(lapply(link_headers, parse_link), recursive = FALSE)
sel <- map_lgl(links, ~ .$rel == rel)
if (sum(sel) != 1L) {
return()
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-resp-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ test_that("can extract specified link url", {
expect_equal(resp_link_url(resp, "first"), NULL)
expect_equal(resp_link_url(response(), "first"), NULL)
})

test_that("can extract from multiple link headers", {
resp <- response(headers = c(
'Link: <https://example.com/1>; rel="next"',
'Link: <https://example.com/2>; rel="last"'
))
expect_equal(resp_link_url(resp, "next"), "https://example.com/1")
expect_equal(resp_link_url(resp, "last"), "https://example.com/2")
})
Loading