Struct mini_server::HTTPResponse
source · pub struct HTTPResponse {
pub body: Vec<u8>,
pub headers: Headers,
pub status: u16,
pub status_text: String,
pub http_version: String,
}
Expand description
The HTTPResponse
struct represents an HTTP response that the web server can send to clients.
It encapsulates various components of an HTTP response, including the response body, headers,
status code, status text, and the HTTP version.
use mini_server::{HTTPResponse, Headers};
fn create_http_response() -> HTTPResponse {
let mut headers = Headers::new();
headers.insert("Content-Type".into(), "text/plain".into());
HTTPResponse {
body: b"Hello, World!".to_vec(),
headers,
status: 200,
status_text: "OK".to_string(),
http_version: "1.1".to_string(),
}
}
Fields§
§body: Vec<u8>
The response body as a vector of bytes.
headers: Headers
A collection of HTTP headers included in the response.
status: u16
The HTTP status code indicating the outcome of the request.
status_text: String
The human-readable status text associated with the status code.
http_version: String
The version of the HTTP protocol used for the response.
Implementations§
source§impl HTTPResponse
impl HTTPResponse
sourcepub fn set_body(&mut self, body: Vec<u8>)
pub fn set_body(&mut self, body: Vec<u8>)
Allows updating the body of an HTTPResponse instance
with a new vector of bytes (Vec<u8>
). Additionally, it automatically
updates the “Content-Length” header to reflect the length of the new body.
sourcepub fn set_headers(&mut self, headers: Headers)
pub fn set_headers(&mut self, headers: Headers)
Update the headers of an HTTPResponse instance with a new set of headers provided as a Headers collection.
sourcepub fn set_header(&mut self, k: &str, v: &str)
pub fn set_header(&mut self, k: &str, v: &str)
Insert/Update the HTTPResponse
header
sourcepub fn set_status(&mut self, status: u16)
pub fn set_status(&mut self, status: u16)
The set_status method allows setting the HTTP status code for an HTTPResponse instance. It updates both the numeric status code (status) and the associated human-readable status text.
sourcepub fn set_version(&mut self, version: String)
pub fn set_version(&mut self, version: String)
Set the HTTPResponse version (e.g. ‘1.1’)
sourcepub fn raw(&mut self) -> Vec<u8> ⓘ
pub fn raw(&mut self) -> Vec<u8> ⓘ
The raw method generates the raw representation of an HTTP response, including
the status line, headers, and body. It returns the
formatted HTTP response as a vector of bytes (Vec<u8>
).
use mini_server::HTTPResponse;
fn get_raw_response(response: &mut HTTPResponse) -> Vec<u8> {
let raw_response = response.raw();
// Accessing the raw HTTP response
println!("Raw Response: {:?}", raw_response);
raw_response
}