pub struct Response {
pub status_code: u16,
pub status_text: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}Expand description
Represents an HTTP response payload and metadata.
You create this type on the response path, add headers, and serialize it to any
Write sink (for example TcpStream or an in-memory buffer in tests).
§Examples
use http_handle::response::Response;
let response = Response::new(200, "OK", b"hello".to_vec());
assert_eq!(response.status_code, 200);§Panics
This type does not panic on construction.
Fields§
§status_code: u16The HTTP status code (e.g., 200 for OK, 404 for Not Found).
status_text: StringThe HTTP status text associated with the status code (e.g., “OK”, “Not Found”).
headers: Vec<(String, String)>A list of headers in the response, each represented as a tuple containing the header name and its corresponding value.
body: Vec<u8>The body of the response, represented as a vector of bytes.
Implementations§
Source§impl Response
impl Response
Sourcepub fn new(status_code: u16, status_text: &str, body: Vec<u8>) -> Self
pub fn new(status_code: u16, status_text: &str, body: Vec<u8>) -> Self
Creates a response with status, reason, and body bytes.
The headers are initialized as an empty list and can be added later using the add_header method.
§Arguments
status_code- The HTTP status code for the response.status_text- The status text corresponding to the status code.body- The body of the response, represented as a vector of bytes.
§Examples
use http_handle::response::Response;
let response = Response::new(204, "NO CONTENT", Vec::new());
assert_eq!(response.status_code, 204);§Panics
This function does not panic.
Sourcepub fn add_header(&mut self, name: &str, value: &str)
pub fn add_header(&mut self, name: &str, value: &str)
Adds a header to the response.
This method allows you to add custom headers to the response, which will be included in the HTTP response when it is sent to the client.
§Examples
use http_handle::response::Response;
let mut response = Response::new(200, "OK", Vec::new());
response.add_header("Content-Type", "text/plain");
assert_eq!(response.headers.len(), 1);§Panics
This function does not panic.
Sourcepub fn send<W: Write>(&self, stream: &mut W) -> Result<(), ServerError>
pub fn send<W: Write>(&self, stream: &mut W) -> Result<(), ServerError>
Sends the response over the provided Write stream.
This method writes the HTTP status line, headers, and body to the stream, ensuring the client receives the complete response.
§Arguments
stream- A mutable reference to any stream that implementsWrite.
§Examples
use http_handle::response::Response;
use std::io::Cursor;
let mut response = Response::new(200, "OK", b"hello".to_vec());
response.add_header("Content-Type", "text/plain");
let mut out = Cursor::new(Vec::<u8>::new());
response.send(&mut out).expect("response write should succeed");
assert!(!out.get_ref().is_empty());§Errors
Returns Err when writing headers or body to the output stream fails.
§Panics
This function does not intentionally panic.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Response
impl<'de> Deserialize<'de> for Response
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Response
impl StructuralPartialEq for Response
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.