Skip to content

Commit 71a5445

Browse files
committed
feat: ✨ added request conversion logic for actix web
1 parent 02ce425 commit 71a5445

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

examples/showcase/server/src/conv_req.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@ use perseus::{HttpRequest, Request};
33
// TODO set up proper error handling in an integration crate
44
/// Converts an Actix Web request into an `http::request`.
55
pub fn convert_req(raw: &actix_web::HttpRequest) -> Result<Request, String> {
6-
let req = HttpRequest::builder()
6+
let mut builder = HttpRequest::builder();
7+
// Add headers one by one
8+
for (name, val) in raw.headers() {
9+
// Each method call consumes and returns `self`, so we re-self-assign
10+
builder = builder.header(name, val);
11+
}
12+
// The URI to which the request was sent
13+
builder = builder.uri(raw.uri());
14+
// The method (e.g. GET, POST, etc.)
15+
builder = builder.method(raw.method());
16+
// The HTTP version used
17+
builder = builder.version(raw.version());
18+
19+
let req = builder
20+
// We always use an empty body because, in a Perseus request, only the URI matters
21+
// Any custom data should therefore be sent in headers (if you're doing that, consider a dedicated API)
722
.body(())
823
.map_err(|err| format!("converting actix web request to perseus-compliant request failed: '{}'", err))?;
924

0 commit comments

Comments
 (0)