我试图使用cURL使用我公司安装的软件的API,以便向另一家必须与之接口的公司提供一些示例。我使用标准的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 ru00e9sultat 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
((它只是输入%uxxxx
字符串(
如果我在Linux服务器上使用cURL,并且JSON输出中的字符显示为Unicode编码,那么完全相同的过程(只需输入重音字符(可以非常好地工作。
唯一的";解决方案";我成功地将数据输入到UTF-8(无BOM(编码的文本文件中,并使cURL使用--data-binary @"<FullFilename>"
:从中读取数据
curl -u ws_redactes:toto https://pas-dev-tmp.grandlyon.fr/api/v2/entite/16/document/4uhM4eg -X PATCH --data-binary @"paramsiparapheur_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 avu00e9 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 u00abMu00e9tadonnu00e9esu00bb est obligatoire."
}
它有效,但很不方便。。。在Windows下还有其他可能性吗?
明白了!
我在另一篇SO文章中读到-d
(或--data
(默认发送内容类型application/x-www-form-urlencoded
,然后我了解了如何对Unicode字符进行URL编码!
我第一次尝试使用Unicode字符的非标准编码(%uxxxx
(输入Unicode代码点,但没有成功。
然后我简单地对UTF-8值进行URL编码,它运行得非常好!
例如,%C3%A9
向API发送Unicode代码点u00e9
(é
(:
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 avu00e9 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 u00abMu00e9tadonnu00e9esu00bb est obligatoire."
}