Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub status: u16,
    pub reason: String,
    pub cookies: HashMap<String, String>,
    pub headers: HashMap<String, String>,
    pub request_headers: HashMap<String, String>,
    pub history: Vec<Response>,
    pub encoding: String,
    pub method: String,
    pub meta: HashMap<String, Value>,
    pub body: Bytes,
    /* private fields */
}
Expand description

HTTP response with lazy-parsed HTML selector.

The response body is stored as raw bytes. The HTML Selector is parsed lazily on first access via selector(). This means creating and inspecting a Response (checking status, reading headers) is cheap – the potentially expensive HTML parse only happens when you need the DOM.

Response implements Send so it can be moved across threads, but the lazy OnceCell storing the parsed selector uses interior mutability, so it is not Sync. Parse on one thread, then share the results.

Fields§

§status: u16

The HTTP status code.

§reason: String

The reason phrase for the status code.

§cookies: HashMap<String, String>

Cookies received in the response.

§headers: HashMap<String, String>

Response headers.

§request_headers: HashMap<String, String>

Headers that were sent with the request.

§history: Vec<Response>

Redirect history leading to this response.

§encoding: String

The character encoding of the response body.

§method: String

The HTTP method used for the request.

§meta: HashMap<String, Value>

Arbitrary metadata associated with this response.

§body: Bytes

The raw response body bytes.

Implementations§

Source§

impl Response

Source

pub fn new( url: &str, body: Bytes, status: u16, reason: Option<String>, cookies: HashMap<String, String>, headers: HashMap<String, String>, request_headers: HashMap<String, String>, encoding: String, method: String, history: Vec<Response>, meta: HashMap<String, Value>, ) -> Self

Creates a new response from its constituent parts. This is primarily used internally by [build_response_async]. Most callers will receive Response objects from Fetcher::get() and similar methods.

Source

pub fn url(&self) -> &str

Returns the final URL of the response after any redirects have been followed. If no redirects occurred, this is the same as the original request URL.

Source

pub fn selector(&self) -> &Selector

Returns the parsed HTML selector, parsing the body on first call.

The parse result is cached, so subsequent calls return immediately. The body is decoded as UTF-8 (with lossy replacement for invalid sequences) before parsing.

Source

pub fn css(&self, query: &str) -> Selectors

Runs a CSS selector query against the parsed HTML and returns matching elements. This is a convenience wrapper around self.selector().css(query). Triggers a lazy parse if the body has not been parsed yet.

Source

pub fn find_by_text( &self, text: &str, partial: bool, case_sensitive: bool, clean_match: bool, ) -> Selectors

Finds elements whose text content matches the given string. Use partial for substring matching, case_sensitive to control case, and clean_match to strip whitespace before comparing.

Source

pub fn text(&self) -> TextHandler

Returns a text handler for extracting and manipulating text content from the parsed HTML. Useful for getting the full visible text of the page.

Source

pub fn urljoin(&self, relative: &str) -> String

Resolves a relative URL against this response’s base URL. For example, if the response URL is https://example.com/page and you pass "/other", this returns "https://example.com/other".

Source

pub fn is_success(&self) -> bool

Returns true if the status code is in the 2xx range (200-299), indicating the request was successfully received, understood, and accepted.

Source

pub fn is_redirect(&self) -> bool

Returns true if the status code is in the 3xx range (300-399). You will only see this when redirect following is disabled, since otherwise the client follows redirects automatically.

Source

pub fn is_client_error(&self) -> bool

Returns true if the status code is in the 4xx range (400-499), indicating a client error such as a bad request, unauthorized access, or not found.

Source

pub fn is_server_error(&self) -> bool

Returns true if the status code is in the 5xx range (500-599), indicating a server-side error. These are often transient and may succeed on retry.

Source

pub fn follow_url(&self, relative: &str) -> String

Resolves a relative URL for following a link. This is a semantic alias for urljoin() that makes crawler code read more naturally.

Source

pub fn to_markdown(&self) -> String

Converts the HTML body to Markdown using the scrapling shell converter. Useful for feeding page content to LLMs or for human-readable text extraction.

Source

pub fn to_text(&self) -> String

Converts the HTML body to plain text, stripping all HTML tags and formatting. Useful when you only care about the visible text content of a page.

Trait Implementations§

Source§

impl Debug for Response

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for Response

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Send for Response

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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