ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.48k stars 891 forks source link

How to set content-type when response some data? #235

Open yummybian opened 7 years ago

yummybian commented 7 years ago

I would like to build a response which contains image data. Where should I set content-type with "image/jpeg"?

ipkn commented 7 years ago

By using a handler with the request and the response arguments or with the return type of response, you can set http headers using response object.

// 1. handler returns response
CROW_ROUTE(...)([]()
{
    res.set_header("Content-Type", "image/jpeg");
    return res;
}

// 2. handler with the response argument and call it's end()
CROW_ROUTE(...)([](const crow::request& req, crow::response& res)
{
    res.set_header("Content-Type", "image/jpeg");
    res.end();
}
abhisharmab commented 7 years ago

@ipkn - Given there is such a method anyways then you should just consider getting rid of the json_value constructor on http_response.