Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub url: Url,
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub body: Bytes,
    pub request_url: Url,
    pub meta: Option<Arc<DashMap<String, Value>>>,
    pub cached: bool,
}
Expand description

Represents an HTTP response received from a server.

Response contains all information about an HTTP response, including the final URL (after redirects), status code, headers, body content, and metadata carried over from the original request.

The type is designed for parse-time ergonomics:

§Example

use spider_util::response::Response;
use reqwest::StatusCode;
use bytes::Bytes;
use url::Url;

let response = Response {
    url: Url::parse("https://example.com").unwrap(),
    status: StatusCode::OK,
    headers: http::header::HeaderMap::new(),
    body: Bytes::from("<html><body>Hello</body></html>"),
    request_url: Url::parse("https://example.com").unwrap(),
    meta: None,
    cached: false,
};

// Parse the response body as HTML
if let Ok(html) = response.to_html() {
    // Process HTML...
}

Fields§

§url: Url

The final URL of the response after any redirects.

§status: StatusCode

The HTTP status code of the response.

§headers: HeaderMap

The headers of the response.

§body: Bytes

The body of the response.

§request_url: Url

The original URL of the request that led to this response.

§meta: Option<Arc<DashMap<String, Value>>>

Metadata associated with the response, carried over from the request. Uses Option to allow lazy initialization.

§cached: bool

Indicates if the response was served from a cache.

Implementations§

Source§

impl Response

Source

pub fn new( url: Url, status: StatusCode, headers: HeaderMap, body: Bytes, request_url: Url, ) -> Self

Creates a new response with an empty HTML cache.

Most application code receives responses from the runtime rather than constructing them directly. This constructor is mainly useful for custom downloaders and lower-level integrations.

Source

pub fn request_from_response(&self) -> Request

Reconstructs the original Request that led to this response.

This method creates a new Request with the same URL and metadata as the request that produced this response. Useful for retry scenarios or when you need to re-request the same resource.

§Example
let original_request = response.request_from_response();
Source

pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>

Deserializes the response body as JSON.

§Type Parameters
  • T: The target type to deserialize into (must implement DeserializeOwned)
§Errors

Returns a serde_json::Error if the body cannot be parsed as JSON or if it cannot be deserialized into type T.

§Example
let data: Data = response.json()?;
Source

pub fn to_html(&self) -> Result<Html, Utf8Error>

Parses the response body as HTML.

Returns a scraper::Html document that can be queried using CSS selectors.

§Errors

Returns a Utf8Error if the response body is not valid UTF-8.

§Example
let html = response.to_html()?;
Source

pub fn lazy_html( &self, ) -> Result<impl Fn() -> Result<Html, Utf8Error> + '_, Utf8Error>

Lazily parses the response body as HTML.

Returns a closure that can be called when the HTML is actually needed. This avoids parsing HTML for responses where it may not be used.

§Errors

Returns a Utf8Error if the response body is not valid UTF-8.

§Example
let html_fn = response.lazy_html()?;
// Parse HTML only when needed
let html = html_fn()?;

Returns a customizable iterator of links discovered in the response body.

Unlike Response::links, this method does not deduplicate results. Callers that need uniqueness can collect into a set or use Response::links.

§Example
let links: Vec<_> = response
    .links_iter(LinkExtractOptions::default())
    .collect();
assert!(!links.is_empty());

Extracts all unique, same-site links from the response body.

This method discovers links from:

  • HTML elements with href or src attributes (<a>, <link>, <script>, <img>, etc.)
  • URLs found in text content (using link detection)

Only links pointing to the same site (same registered domain) are included.

§Returns

A DashSet of Link objects containing the URL and link type.

§Example
let links = response.links();
for link in links.iter() {
    println!("Found {:?} link: {}", link.link_type, link.url);
}

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more