I'm reading the examples of actix-web, but as I am quite new to Rust I am having some issues understanding how to adapt the code to my needs.
Given an actix-web HttpRequest
, I want to parse the payload and return a JsonValue
. I can't figure out how to change this function to return the JsonValue
rather than a HttpResponse
.
fn index_mjsonrust(req: &HttpRequest, ) -> Box<Future<Item = HttpResponse, Error = Error>> {
req.payload()
.concat2()
.from_err()
.and_then(|body| {
// body is loaded, now we can deserialize json-rust
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result
let injson: JsonValue = match result {
Ok(v) => v,
Err(e) => object!{"err" => e.to_string() },
};
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(injson.dump()))
})
.responder()
}
Would it be better to just return the JsonValue
rather than a Future
?
You have to convert
JsonValue
to a string or bytes, then you can set it as theHttpResponse
body. You can not directly return aJsonValue
instead of box because the request body reading process is asynchronous.