pub struct HttpResponse {
pub headers: ResponseHeaders,
/* private fields */
}Expand description
Represents an HTTP response being sent to the client.
The HttpResponse struct provides methods to construct and manipulate HTTP responses including status codes, headers, cookies, and different types of response bodies.
§Examples
Basic usage:
use ripress::context::HttpResponse;
let res = HttpResponse::new();
res.ok().text("Hello, World!");JSON response:
use ripress::context::HttpResponse;
use serde_json::json;
let res = HttpResponse::new();
res.ok().json(json!({
"message": "Success",
"code": 200
}));§Fields
status_code- HTTP status code (e.g., 200, 404, 500)body- Response body content (JSON, text)content_type- Content-Type header valuecookies- Response cookies to be setheaders- Response headersremove_cookies- Cookies to be removed
Fields§
§headers: ResponseHeadersSets response headers
Implementations§
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn no_content(self) -> Self
pub fn no_content(self) -> Self
Sets the status code to 204 No Content.
Sourcepub fn bad_request(self) -> Self
pub fn bad_request(self) -> Self
Sets the status code to 400 Bad Request.
Sets the status code to 401 Unauthorized.
Sourcepub fn method_not_allowed(self) -> Self
pub fn method_not_allowed(self) -> Self
Sets the status code to 405 Method Not Allowed.
Sourcepub fn internal_server_error(self) -> Self
pub fn internal_server_error(self) -> Self
Sets the status code to 500 Internal Server Error.
Sourcepub fn not_implemented(self) -> Self
pub fn not_implemented(self) -> Self
Sets the status code to 501 Not Implemented.
Sourcepub fn bad_gateway(self) -> Self
pub fn bad_gateway(self) -> Self
Sets the status code to 502 Bad Gateway.
Sets the status code to 503 Service Unavailable.
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Returns the current HTTP status code as a u16.
Sourcepub fn text<T: Into<String>>(self, text: T) -> Self
pub fn text<T: Into<String>>(self, text: T) -> Self
Sets the response body to text.
§Arguments
text- Any type that can be converted into a String
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
let res = HttpResponse::new()
.ok()
.text("Operation completed successfully");
// Using with different types
let res = HttpResponse::new()
.ok()
.text(format!("Count: {}", 42));Sourcepub fn json<T: Serialize>(self, json: T) -> Self
pub fn json<T: Serialize>(self, json: T) -> Self
Sets the response body to JSON.
§Arguments
json- Any type that implementsserde::Serialize
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
use serde::Serialize;
#[derive(Serialize)]
struct User {
name: String,
age: u32,
}
let user = User {
name: "John".to_string(),
age: 30,
};
let res = HttpResponse::new()
.ok()
.json(user);Sourcepub fn bytes<T: Into<Bytes>>(self, bytes: T) -> Self
pub fn bytes<T: Into<Bytes>>(self, bytes: T) -> Self
Sets the response body to binary data.
§Arguments
bytes- Any type that can be converted intoBytes
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
use bytes::Bytes;
let data = vec![1, 2, 3, 4, 5];
let res = HttpResponse::new()
.ok()
.bytes(data);
// Using with Bytes directly
let res = HttpResponse::new()
.ok()
.bytes(Bytes::from_static(b"hello world"));Sourcepub fn set_header<K, V>(self, header_name: K, header_value: V) -> Self
pub fn set_header<K, V>(self, header_name: K, header_value: V) -> Self
Sets a header in the response.
§Example
use ripress::context::HttpResponse;use ripress::context::HttpResponse; let res = HttpResponse::new(); res.set_header(“key”, “value”); // Sets the key cookie to value
Sets a cookie in the response.
§Arguments
key- The name of the cookievalue- The value to set
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
use ripress::res::response_cookie::CookieOptions;
let res = HttpResponse::new()
.set_cookie("session", "abc123", None)
.ok()
.text("Logged in");Sourcepub fn permanent_redirect(self, path: &'static str) -> Self
pub fn permanent_redirect(self, path: &'static str) -> Self
Sourcepub fn html(self, html: &str) -> Self
pub fn html(self, html: &str) -> Self
Sets the response body to html.
§Arguments
html_content- Any type that can be converted into a String
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
let res = HttpResponse::new()
.ok()
.html("<h1>Hello, World!</h1>");
// Using with different types
let res = HttpResponse::new()
.ok()
.text(format!("<h1>Count: {}</h1>", 42));Sourcepub async fn send_file(self, path: &'static str) -> Self
pub async fn send_file(self, path: &'static str) -> Self
Sends the contents of a file as the response body.
This method reads the file at the given path asynchronously and sets the response body to its contents.
The content type is inferred from the file’s bytes using the infer crate and then mapped to a MIME
type via mime_guess. If the type cannot be determined, it falls back to application/octet-stream.
§Arguments
path- The path to the file to be sent. Must be a static string slice.
§Returns
Returns Self for method chaining.
§Example
use ripress::context::HttpResponse;
use ripress::context::HttpRequest;
async fn handler(req: HttpRequest, res: HttpResponse) -> HttpResponse {
// Send a file as the response
res.ok().send_file("static/image.png").await
}Sourcepub fn write<S, E>(self, stream: S) -> Self
pub fn write<S, E>(self, stream: S) -> Self
Streams the response
§Arguments
stream- The stream to stream
§Returns
Returns Self for method chaining
§Example
use ripress::context::HttpResponse;
use bytes::Bytes;
use futures::stream;
use futures::StreamExt;
let res = HttpResponse::new();
let stream = stream::iter(0..5).map(|n| Ok::<Bytes, std::io::Error>(Bytes::from(format!("Number: {}\n", n))));
res.write(stream);