I have a warp server running, and for every request I need to compute a string and then return that string with a particular status code.
use warp::{http::StatusCode, reply, Filter};
let creator = warp::post()
.and(warp::path("mypath"))
.and(warp::body::bytes())
.and(warp::header("myheader"))
.map(move |buf: warp::hyper::body::Bytes, header: String| {
if (is_request_invalid()) {
reply::with_status("Request parameter xyz is invalid", StatusCode::BAD_REQUEST);
}
let computed_string: String = compute_from(buf, header);
return reply::with_status(
computed_string,
StatusCode::CREATED,
);
});
However, this doesn't work because reply::with_status requires a type of &str
.
error[E0308]: mismatched types
--> lib.rs:54:33
|
54 | ... computed_string,
| ^^^^^^^^^^^^^^^
| |
| expected `&str`, found struct `std::string::String`
| help: consider borrowing here: `&computed_string`
So I tried:
// -- SNIP --
return reply::with_status(
&computed_string,
StatusCode::CREATED,
);
// -- SNIP --
(since &String derefs to &str) -- but this doesn't work either, as you can't return a reference to a variable owned by the current scope (will be dropped)
error[E0597]: `computed_string` does not live long enough
--> lib.rs:54:33
|
53 | return reply::with_status(
| ____________________________________-
54 | | &computed_string,
| | ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
55 | | StatusCode::CREATED,
56 | | );
| |_____________________________- argument requires that `computed_string` is borrowed for `'static`
57 | }
| - `computed_string` dropped here while still borrowed
How do I return a String
as a response when using warp
?
The question didn't originally include the complete code, however it turns out there was a previous early-return which included an &str
, which resulted in reply::with_status
returning a WithStatus<&str>
, which caused a mismatch when trying to return a WithStatus<String>
in the same scope.
use warp::{http::StatusCode, reply, Filter};
let creator = warp::post()
.and(warp::path("mypath"))
.and(warp::body::bytes())
.and(warp::header("myheader"))
.map(move |buf: warp::hyper::body::Bytes, header: String| {
if (is_request_invalid()) {
return reply::with_status("Request parameter xyz is invalid", StatusCode::BAD_REQUEST);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type &str
}
let computed_string: String = compute_from(buf, header);
return reply::with_status(
computed_string,
// ^^^^^^^^^^^^^^^ type String
StatusCode::CREATED,
);
});