microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.89k stars 1.63k forks source link

Non-ASCII characters not converted properly in http_response.extract_utf8_string in MSVC 2019 #1756

Open jdx-john opened 1 year ago

jdx-john commented 1 year ago

My server is returning the following as shown in PostMan:

image

{
  "status": "FEL: Det valda objektet för händelsen"
}

In C++ VS2019, reading as a wstring is perfect but if I call response.extract_utf8_string() the JSON has messed up characters, in exactly the same way if I use utility::conversions:

    std::string status = "unknown";
    if (json.has_string_field(L"status"))
    {
        auto tmp = json[L"status"].as_string();
        status = utility::conversions::to_utf8string(tmp);
    }

tmp: L"FEL: Det valda objektet för händelsen" status: "FEL: Det valda objektet för händelsen"

I need to use this value in a std::exception which only takes std::string what is the correct way to convert and why isn't utf8 working in the first place, when that's how the content is being encoded in the response?