diff --git a/NEWS.md b/NEWS.md index bc1fb892..409052fe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/resp-headers.R b/R/resp-headers.R index 0560fc33..d76f18bb 100644 --- a/R/resp-headers.R +++ b/R/resp-headers.R @@ -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() diff --git a/tests/testthat/test-resp-headers.R b/tests/testthat/test-resp-headers.R index 52c9c42e..cfc3070f 100644 --- a/tests/testthat/test-resp-headers.R +++ b/tests/testthat/test-resp-headers.R @@ -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: ; rel="next"', + 'Link: ; rel="last"' + )) + expect_equal(resp_link_url(resp, "next"), "https://example.com/1") + expect_equal(resp_link_url(resp, "last"), "https://example.com/2") +})