pub struct Response { /* private fields */ }Expand description
Represents a parsed HTTP response.
Implementations§
Source§impl Response
impl Response
Sourcepub fn get_header<K>(&self, key: K) -> OptionResponseHeadersValuewhere
K: Into<ResponseHeadersKey>,
pub fn get_header<K>(&self, key: K) -> OptionResponseHeadersValuewhere
K: Into<ResponseHeadersKey>,
Sourcepub fn get_header_front<K>(&self, key: K) -> OptionResponseHeadersValueItemwhere
K: Into<ResponseHeadersKey>,
pub fn get_header_front<K>(&self, key: K) -> OptionResponseHeadersValueItemwhere
K: Into<ResponseHeadersKey>,
Retrieves the first value of a response header by its key.
§Parameters
key: The header’s key, which can be of any type that implementsInto<ResponseHeadersKey>.
§Returns
OptionResponseHeadersValueItem: ReturnsSome(value)if the key exists and has at least one value, orNoneif the key does not exist or has no values.
Sourcepub fn get_header_back<K>(&self, key: K) -> OptionResponseHeadersValueItemwhere
K: Into<ResponseHeadersKey>,
pub fn get_header_back<K>(&self, key: K) -> OptionResponseHeadersValueItemwhere
K: Into<ResponseHeadersKey>,
Retrieves the last value of a response header by its key.
§Parameters
key: The header’s key, which can be of any type that implementsInto<ResponseHeadersKey>.
§Returns
OptionResponseHeadersValueItem: ReturnsSome(value)if the key exists and has at least one value, orNoneif the key does not exist or has no values.
Sourcepub fn has_header<K>(&self, key: K) -> boolwhere
K: Into<ResponseHeadersKey>,
pub fn has_header<K>(&self, key: K) -> boolwhere
K: Into<ResponseHeadersKey>,
Sourcepub fn has_header_value<K, V>(&self, key: K, value: V) -> bool
pub fn has_header_value<K, V>(&self, key: K, value: V) -> bool
Checks if a header contains a specific value.
§Parameters
key: The header key to check, which will be converted into aResponseHeadersKey.value: The value to search for in the header.
§Returns
true: If the header exists and contains the specified value.false: If the header does not exist or does not contain the value.
Sourcepub fn get_headers_len(&self) -> usize
pub fn get_headers_len(&self) -> usize
Gets the number of headers in the response.
§Returns
- The number of unique header keys in the response.
Sourcepub fn get_header_len<K>(&self, key: K) -> usizewhere
K: Into<ResponseHeadersKey>,
pub fn get_header_len<K>(&self, key: K) -> usizewhere
K: Into<ResponseHeadersKey>,
Sourcepub fn get_headers_values_len(&self) -> usize
pub fn get_headers_values_len(&self) -> usize
Gets the total number of header values in the response.
This counts all values across all headers, so a header with multiple values will contribute more than one to the total count.
§Returns
- The total number of header values in the response.
Sourcepub fn get_body_string(&self) -> String
pub fn get_body_string(&self) -> String
Retrieves the body content of the object as a UTF-8 encoded string.
This method uses String::from_utf8_lossy to convert the byte slice returned by self.get_body() into a string.
If the byte slice contains invalid UTF-8 sequences, they will be replaced with the Unicode replacement character (�).
§Returns
A String containing the body content.
Sourcepub fn get_body_json<T>(&self) -> ResultJsonError<T>where
T: DeserializeOwned,
pub fn get_body_json<T>(&self) -> ResultJsonError<T>where
T: DeserializeOwned,
Deserializes the body content of the object into a specified type T.
This method first retrieves the body content as a UTF-8 encoded string using self.get_body().
It then attempts to deserialize the string into the specified type T using json_from_slice.
§Type Parameters
T: The target type to deserialize into. It must implement theDeserializeOwnedtrait.
§Returns
Ok(T): The deserialized object of typeTif the deserialization is successful.Err(ResultJsonError): An error if the deserialization fails (e.g., invalid JSON format or type mismatch).
Sourcepub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
Adds a header to the response.
This function appends a value to the response headers. If the header already exists, the new value will be added to the existing values.
§Parameters
key: The header key, which will be converted into aResponseHeadersKey.value: The value of the header, which will be converted into a String.
Sourcepub fn replace_header<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn replace_header<K, V>(&mut self, key: K, value: V) -> &mut Self
Replaces all values for a header in the response.
This function replaces all existing values for a header with a single new value.
§Parameters
key: The header key, which will be converted into aResponseHeadersKey.value: The value of the header, which will be converted into a String.
Sourcepub fn remove_header<K>(&mut self, key: K) -> &mut Selfwhere
K: Into<ResponseHeadersKey>,
pub fn remove_header<K>(&mut self, key: K) -> &mut Selfwhere
K: Into<ResponseHeadersKey>,
Removes a header from the response.
This function removes all values for the specified header key.
§Parameters
key: The header key to remove, which will be converted into aResponseHeadersKey.
Sourcepub fn remove_header_value<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn remove_header_value<K, V>(&mut self, key: K, value: V) -> &mut Self
Removes a specific value from a header in the response.
This function removes only the specified value from the header. If the header has multiple values, only the matching value is removed. If this was the last value for the header, the entire header is removed.
§Parameters
key: The header key, which will be converted into aResponseHeadersKey.value: The specific value to remove from the header.
Sourcepub fn clear_headers(&mut self) -> &mut Self
pub fn clear_headers(&mut self) -> &mut Self
Clears all headers from the response.
This function removes all headers, leaving the headers map empty.
Sourcepub fn set_body<T>(&mut self, body: T) -> &mut Selfwhere
T: Into<ResponseBody>,
pub fn set_body<T>(&mut self, body: T) -> &mut Selfwhere
T: Into<ResponseBody>,
Set the body of the response.
This method allows you to set the body of the response by converting the provided
value into a ResponseBody type. The body is updated with the converted value,
and the method returns a mutable reference to the current instance for method chaining.
§Parameters
body: The body of the response to be set. It can be any type that can be converted into aResponseBodyusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining. Set the body of the response.
This method allows you to set the body of the response by converting the provided
value into a ResponseBody type. The body is updated with the converted value,
and the method returns a mutable reference to the current instance for method chaining.
§Parameters
body: The body of the response to be set. It can be any type that can be converted into aResponseBodyusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn set_reason_phrase<T>(&mut self, reason_phrase: T) -> &mut Selfwhere
T: Into<ResponseReasonPhrase>,
pub fn set_reason_phrase<T>(&mut self, reason_phrase: T) -> &mut Selfwhere
T: Into<ResponseReasonPhrase>,
Set the reason phrase of the response.
This method allows you to set the reason phrase of the response by converting the
provided value into a ResponseReasonPhrase type. The reason_phrase is updated
with the converted value, and the method returns a mutable reference to the current
instance for method chaining.
§Parameters
reason_phrase: The reason phrase to be set for the response. It can be any type that can be converted into aResponseReasonPhraseusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn build(&mut self) -> ResponseData
pub fn build(&mut self) -> ResponseData
Sourcepub fn get_string(&self) -> String
pub fn get_string(&self) -> String
Converts the response to a formatted string representation.
- Returns: A
Stringcontaining formatted response details.