c++nghttp2

nghttp2-asio : Proper way to set content-type in header


I came up with this code, but not sure if it is right usage:

    boost::system::error_code ec;

    nghttp2::asio_http2::header_map headers;
    headers.insert(std::pair<std::string, nghttp2::asio_http2::header_value>("content-type", {"application/json; charset=utf-8", false}));
    headers.insert(std::pair<std::string, nghttp2::asio_http2::header_value>("content-length",{std::to_string(r.length()), false}));

    auto req = session->submit(ec, "POST", uri, r.data(), headers);

Could anybody tell me if this is correct usage.

Thanks


Solution

  • Yes, what you have done is correct way of setting a header in nghttp2 asio. Please read the documentation to know the usage of the sensitive field here.

    For brevity, you can use std::make_pair. For example:

    headers.insert(std::make_pair("content-type", {"application/json; charset=utf-8", false}));