0

As I understand it, middleware handlers are not suitable for this since they work before processing the request. My current solution is:

router.get("/items/:id", middleware! { |request, mut response|
    // ...
    let data = json::encode(&data).unwrap();
    response.set(ContentLength(data.len() as u64));
    data
});

I do not like this because I have many routes and I will have to insert this code in each of them. I would like a middleware which would be called after processing my routes. Is there another way?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Ilya
  • 51
  • 6
  • *since they work before processing the request* — what makes you think this? Looking at [`Middleware`](https://docs.rs/nickel/0.10.0/nickel/trait.Middleware.html), it seems like you are able to change the `Response` however you'd like. Additionally, there appears to be [built-in support for responding with JSON](https://docs.rs/nickel/0.10.0/nickel/trait.Responder.html) and [examples](https://github.com/nickel-org/nickel.rs/blob/master/examples/json.rs); does that not set the content length for you? – Shepmaster Aug 27 '17 at 16:11
  • I think so because of this - [nickel.rs#L109](https://github.com/nickel-org/nickel.rs/blob/d5b9271b18d22e858c9dc858ef52deef95d7f11a/src/nickel.rs#L109). Responder looks like what I need, but I do not understand how to use it. This was my first day learning this language. C: – Ilya Aug 27 '17 at 17:39
  • I know about json support, but I don't know how use this knowledge for write content length. ) – Ilya Aug 27 '17 at 17:48

0 Answers0