如何在 Windows 命令行中使用 cURL 发送 Unicode 字符?

发布于 2025-01-16 06:33:42 字数 2477 浏览 2 评论 0原文

我尝试使用我公司安装的软件的 API(使用 cURL),以便向另一家必须与其交互的公司提供一些示例。我使用标准 Windows 命令行来执行此操作。

它工作得很好,直到我尝试使用包含重音字符的数据更新一些参数(我在法国)。

这是我尝试这样做时收到的错误消息:

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"
}

因此,API 显然正在等待 Unicode 数据,而我只是使用键盘输入重音字符。

我尝试了很多解决方案来解决这个问题,但没有一个有效:

  • 使用 chcp 65001 更改页面代码:同样的问题。
  • 使用“UnicodeInput”应用程序输入 Unicode 字符:重音字符出现在命令行窗口中,但我仍然收到相同的错误消息。
  • 使用另一个终端(Cmder):同样的问题。
  • 从 Notepad++ 打开的 UTF-8 文本文件复制/粘贴:同样的问题。
  • 使用 URL 编码:虽然支持 URL 编码(我通过输入 %21 获得 ! 字符),但 Unicode 字符的非标准编码 (%uxxxx< /code>)似乎不受支持(它只是输入 %uxxxx 字符串)。

如果我在 Linux 服务器上使用 cURL 并且字符在 JSON 输出中以 Unicode 编码,则完全相同的过程(只需输入重音字符)就可以完美地工作。

我成功找到工作的唯一“解决方案”是将数据输入到 UTF-8(无 BOM)编码的文本文件中,并让 cURL 使用 --data-binary @"从中读取数据。 ":

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."
}

可以,但是很不方便……Windows下还有其他可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2025-01-23 06:33:42

知道了!

我在另一篇 SO 帖子中读到 -d (或 --data)发送 Content-Type application/x-www-form-urlencoded默认情况下,然后我看看如何对 Unicode 字符进行 URL 编码!

我首先尝试使用 Unicode 字符的非标准编码 (%uxxxx) 输入 Unicode 代码点,但没有成功。

然后我简单地对 UTF-8 值进行 URL 编码,它工作得很好!

例如,%C3%A9 将 Unicode 代码点 \u00e9 (é) 发送到 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."
}

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."
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文