http_request

Struct HttpResponse

Source
pub struct HttpResponse {
    pub http_version: String,
    pub status_code: u16,
    pub status_text: String,
    pub headers: HashMap<String, String>,
    pub body: Vec<u8>,
}
Expand description

A struct representing an HTTP response.

This struct contains all the components of an HTTP response: the HTTP version, status code, status text, headers, and body. It is used to model and manipulate HTTP responses within the application.

§Fields

  • http_version: A string representing the HTTP version (e.g., “HTTP/1.1”).
  • status_code: The HTTP status code (e.g., 200 for OK, 404 for Not Found).
  • status_text: A string containing the status text associated with the status code (e.g., “OK”, “Not Found”).
  • headers: A HashMap<String, String> containing the headers of the response, where each key is the header name (e.g., “Content-Type”), and the value is the corresponding header value.
  • body: A Vec<u8> representing the body of the HTTP response, which contains the content being returned.

Fields§

§http_version: String

The HTTP version of the response (e.g., “HTTP/1.1”).

§status_code: u16

The HTTP status code (e.g., 200, 404).

§status_text: String

The status text associated with the status code (e.g., “OK”, “Not Found”).

§headers: HashMap<String, String>

A HashMap of headers, where the key is the header name and the value is the header value.

§body: Vec<u8>

The body of the response, which contains the content being returned.

Implementations§

Source§

impl HttpResponse

Provides functionality for parsing and working with HTTP responses.

This implementation contains methods for extracting specific information from HTTP response strings, such as content length, and parsing the entire response into an HttpResponse object.

§Methods

  • get_content_length: Extracts the Content-Length value from the HTTP response string.
  • from: Parses a raw HTTP response string into an HttpResponse struct, including the status line, headers, and body.
Source

pub fn get_content_length(response_string: &str) -> usize

Extracts the Content-Length from the response string.

This method scans the HTTP response string for the Content-Length header and parses its value into a usize. If the header is not present or its value is invalid, the method returns 0 as a default.

§Parameters
  • response_string: A string representing the HTTP response.
§Returns

Returns the Content-Length value extracted from the response, or 0 if not found.

Source

pub fn from(response: &[u8]) -> Self

Parses an HTTP response from a byte slice and returns an HttpResponse object.

This function processes the raw HTTP response in byte form. It splits the response into the status line, headers, and body, parsing each part accordingly. The status line is parsed to extract the HTTP version, status code, and status text. Headers are split and stored in a HashMap. The body is collected into a byte vector.

§Parameters
  • response: A byte slice representing the raw HTTP response.
§Returns

Returns an HttpResponse object containing the parsed HTTP version, status code, status text, headers, and body. If parsing any part fails, defaults are used (e.g., HTTP/1.1, status code 200).

§Panics

This method will panic if the HTTP response is malformed in ways that the unwrap operations cannot handle.

Source

pub fn text(self) -> HttpResponseText

Converts the response body to text format.

This function takes the current response and creates a new HttpResponse instance with the body converted to a text representation. The body is extracted as text from the original response body and stored in the new response as a ResponseBody::Text variant.

§Returns
  • Self - A new HttpResponse instance with the body converted to text.

Trait Implementations§

Source§

impl Clone for HttpResponse

Source§

fn clone(&self) -> HttpResponse

Returns a copy 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 HttpResponse

Source§

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

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

impl Default for HttpResponse

Default implementation for HttpResponse.

This implementation provides default values for an HttpResponse instance, setting the HTTP version to the default version, the status code to StatusCode::Unknown, and initializing the headers and body to empty collections.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for HttpResponse

Source§

fn eq(&self, other: &HttpResponse) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for HttpResponse

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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T