Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Link Header Parsing

aioduct can parse Link headers (RFC 8288) from HTTP responses. Link headers are commonly used for pagination, resource discovery, and relation metadata.

Use Response::links() to extract all Link header values:

#![allow(unused)]
fn main() {
use aioduct::{TokioClient, Link};

async fn example() -> Result<(), aioduct::Error> {
let client = TokioClient::new();
let resp = client.get("https://api.example.com/items?page=1")?
    .send()
    .await?;

for link in resp.links() {
    println!("URI: {}", link.uri);
    if let Some(ref rel) = link.rel {
        println!("  rel: {rel}");
    }
}
Ok(())
}
}

The Link struct contains:

FieldTypeDescription
uriStringThe target URI
relOption<String>Relation type (e.g., next, prev, last)
titleOption<String>Human-readable title
media_typeOption<String>Expected media type of the target
anchorOption<String>Context URI for the link

Common Patterns

Pagination

Many APIs use Link headers for pagination:

Link: <https://api.example.com/items?page=2>; rel="next",
      <https://api.example.com/items?page=5>; rel="last"
#![allow(unused)]
fn main() {
use aioduct::response::Response;
fn next_page_url(resp: &Response) -> Option<String> {
    resp.links()
        .into_iter()
        .find(|l| l.rel.as_deref() == Some("next"))
        .map(|l| l.uri)
}
}

Direct Parsing

You can also parse Link headers directly from a HeaderMap:

#![allow(unused)]
fn main() {
use aioduct::link::parse_link_headers;
use http::HeaderMap;

let mut headers = HeaderMap::new();
headers.insert(
    "link",
    "<https://example.com>; rel=\"canonical\"".parse().unwrap(),
);

let links = parse_link_headers(&headers);
assert_eq!(links[0].rel.as_deref(), Some("canonical"));
}