How to send Unicode characters using cURL in Windows command line?

Viewed 708

I try to use the API of a software installed in my company using cURL in order to give some examples to another company which will have to interface with it. I use standard Windows Command Line to do this.

It works perfectly well, until I try to update some parameters with data containing accented characters (I'm in France).

This is the error message I get when trying to do so:

curl -u ws_redactes:toto https://pas-dev-tmp.grandlyon.fr/api/v2/entite/16/document/IlnZUw4 -X PATCH -d "nom_vp_signataire=Test avé l'assent"
{
    "status": "error",
    "error-message": "Impossible d'encoder le r\u00e9sultat en JSON [code 5]: Malformed UTF-8 characters, possibly incorrectly encoded"
}

Therefore, the API is obviously waiting for Unicode data while I simply entered the accented characters using my keyboard.

I tried many solutions to get around this, none of them worked:

  • Changing the pagecode using chcp 65001: same issue.
  • Using the "UnicodeInput" application to input a Unicode character: the accented character appears in the command line windows but I still get the same error message.
  • Using another terminal (Cmder): same issue.
  • Copy/Paste from a UTF-8 text file opened in Notepad++: same issue.
  • Using URL encoding: while URL encoding is supported (I get the ! character by entering %21), the non-standard encoding for Unicode characters (%uxxxx) seems not to be supported (it simply inputs the %uxxxx string).

The exact same procedure (by simply entering the accented characters) works perfectly fine if I use cURL on a Linux server and the characters appears encoded in Unicode in the JSON output.

The only "solution" I was successful at getting work is to enter the data into a UTF-8 (without BOM) encoded text file and makes cURL reads data from it using --data-binary @"<FullFilename>":

curl -u ws_redactes:toto https://pas-dev-tmp.grandlyon.fr/api/v2/entite/16/document/4uhM4eg -X PATCH --data-binary @"params\iparapheur_type.txt"
{
    "content": {
        "info": {
            "id_d": "4uhM4eg",
            "type": "aide-pierre-public-decision-v2",
            "titre": "",
            "creation": "2022-04-06 11:49:50",
            "modification": "2022-04-06 11:50:00"
        },
        "data": {
            "envoi_transformation": "checked",
            "envoi_signature": "checked",
            "envoi_sae": "checked",
            "envoi_iparapheur": "1",
            "envoi_fast": "",
            "nom_vp_signataire": "Test av\u00e9 l'assent"
        },
        "action_possible": [
            "modification",
            "supression"
        ],
        "action-possible": [
            "modification",
            "supression"
        ],
        "last_action": {
            "action": "modification",
            "message": "Modification du document",
            "date": "2022-04-06 11:50:00"
        }
    },
    "result": "ok",
    "formulaire_ok": 0,
    "message": "Le formulaire est incomplet : le champ \u00abM\u00e9tadonn\u00e9es\u00bb est obligatoire."
}

It works but it's very inconvenient... Is there any other possibility under Windows?

1 Answers

Got it!

I read on another SO post that -d (or --data) sends the Content-Type application/x-www-form-urlencoded by default, then I had a look at how to URL encode Unicode characters!

I first tried to enter the Unicode code point with the non-standard encoding for Unicode characters (%uxxxx) but it didn't work.

Then I simply URL encoded the UTF-8 value and it works perfectly fine!

For instance, %C3%A9 sends the Unicode code point \u00e9 (é) to the API:

curl -u ws_redactes:toto https://pas-dev-tmp.grandlyon.fr/api/v2/entite/16/document/1Z4fZBM -X PATCH -d "nom_vp_signataire=Test av%C3%A9 l'assent"
{
    "content": {
        "info": {
            "id_d": "1Z4fZBM",
            "type": "aide-pierre-public-decision-v2",
            "titre": "",
            "creation": "2022-04-06 09:28:20",
            "modification": "2022-04-06 09:33:08"
        },
        "data": {
            "envoi_transformation": "checked",
            "envoi_signature": "checked",
            "envoi_sae": "checked",
            "envoi_iparapheur": "1",
            "envoi_fast": "",
            "nom_vp_signataire": "Test av\u00e9 l'assent"
        },
        "action_possible": [
            "modification",
            "supression"
        ],
        "action-possible": [
            "modification",
            "supression"
        ],
        "last_action": {
            "action": "modification",
            "message": "Modification du document",
            "date": "2022-04-06 09:33:08"
        }
    },
    "result": "ok",
    "formulaire_ok": 0,
    "message": "Le formulaire est incomplet : le champ \u00abM\u00e9tadonn\u00e9es\u00bb est obligatoire."
}
Related