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: u16The HTTP status code.
reason: StringThe reason phrase for the status code.
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: StringThe character encoding of the response body.
method: StringThe HTTP method used for the request.
meta: HashMap<String, Value>Arbitrary metadata associated with this response.
body: BytesThe raw response body bytes.
Implementations§
Source§impl Response
impl Response
Sourcepub 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
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.
Sourcepub fn url(&self) -> &str
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.
Sourcepub fn selector(&self) -> &Selector
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.
Sourcepub fn css(&self, query: &str) -> Selectors
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.
Sourcepub fn find_by_text(
&self,
text: &str,
partial: bool,
case_sensitive: bool,
clean_match: bool,
) -> Selectors
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.
Sourcepub fn text(&self) -> TextHandler
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.
Sourcepub fn urljoin(&self, relative: &str) -> String
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".
Sourcepub fn is_success(&self) -> bool
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.
Sourcepub fn is_redirect(&self) -> bool
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.
Sourcepub fn is_client_error(&self) -> bool
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.
Sourcepub fn is_server_error(&self) -> bool
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.
Sourcepub fn follow_url(&self, relative: &str) -> String
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.
Sourcepub fn to_markdown(&self) -> String
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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Response
impl !RefUnwindSafe for Response
impl !Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
impl !UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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