3

I need to send a UTF-8 string with Unicode characters in it to a web API from a c/c++ application, I am sending it with a Content-Type of application/json.

For example I have a string that is "щь".

Currently what sends is the ASCII representation (each individual byte) "щь"

However I would like it to send the Unicode number instead "\u0449\u044c"

Is there a way to do this using WinHTTPRequest?

Steven
  • 41
  • 3
  • Not a duplicate because it seems to be going the other way, but could have useful information: https://stackoverflow.com/q/21503314/10957435 –  Sep 27 '19 at 19:16
  • @Chipster Thanks I'll take a look! – Steven Sep 27 '19 at 19:34
  • Regrettably the method employed in that post does not work for this situation. Converting the multi byte I currently have have to a wide string and Posting it through WinHttpSendRequest results in malformed characters in the request. – Steven Sep 27 '19 at 20:33
  • `"щь"` is the correct UTF-8 encoded form of `"щь"`, just misinterpreted as Latin-1 instead of UTF-8. The actual raw bytes are correct. WinHTTP sends whatever bytes you give it, make sure to specify `charset=utf-8` in your `Content-Type` request header. If the server ends up with `"щь"` instead of `"щь"` when parsing the data then it is not interpreting the UTF-8 properly. That is not a problem on your client side. Otherwise, simply re-format your JSON string to use explicit `"\uXXXX"` encoded substrings instead of raw Unicode chars, eg: `"field": "\u0449\u044C"` instead of `"field": "щь"`. – Remy Lebeau Sep 27 '19 at 20:50
  • The JSON string that I am using is dynamically, do you know if there is a way to retrieving or converting to the unicode number (\u0449) from the "щь" or "щь" – Steven Sep 27 '19 at 21:01
  • @Steven that is a feature of JSON, so find a JSON library that outputs what you want. Or simply parse the JSON yourself to convert it as desired. Either way, this issue has nothing to do with WinHTTP at all. – Remy Lebeau Sep 28 '19 at 08:52
  • @RemyLebeau Thank you for your help! I have been able to get it working now. – Steven Sep 30 '19 at 14:05

0 Answers0