hyper-staticfile icon indicating copy to clipboard operation
hyper-staticfile copied to clipboard

Serving either JSON string or static file (hyper 1.0+)

Open eldenpark opened this issue 1 year ago • 1 comments

Previously with hyper 0.14, I was able to design a server with hyper and hyper-staticfile (0.9.x) to serve different types of requests, depending on the URI that requests plain JSON string or the others for static files.

Now with the new version of hyper-staticfile w/ hyper 1.0+, it became a bit more difficult. Body enum seems to have Empty or Stream variants so this by default can't handle { "foo": "1" } type of response. Any idea where I should work to make this ideally work?

eldenpark avatar Feb 14 '24 16:02 eldenpark

I think Response::map might work for you? https://docs.rs/http/1.0.0/http/response/struct.Response.html#method.map

Alternatively, you can do into_parts + from_parts: https://docs.rs/http/1.0.0/http/response/struct.Response.html#method.from_parts

Both should allow you to create a new Response with a different body type, while preserving all the headers, etc.

stephank avatar Feb 16 '24 10:02 stephank