欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

javascript - 如何用curl post 一段包含中文json的文本到服务器?

程序员文章站 2022-06-05 09:44:47
...
CURL 命令这么写的.
curl -X POST http://localhost:3000/api/licenses/eee -d itemdata='{"nType":"1","nBaoYou":"0","szItemId":"536347062406","title":"秋冬新款女装韩版半高领毛 衣女加厚修身显瘦长袖套头针织衫打底衫","secondTitle":"","imgURL":"http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg","promotionTxt":"限时秒杀价","fOldPrice":"108","fThirdPrice":"0","fCommissionPer":"10.5","fCommission":"2.08","nTgCount":"0","nSellCount":"37","fPingFen":"0","nPingjiaCount":"5","szTgURL":"","szWebTgURL":"","szBlogTgURL":"","shortUrl":"","szDesc":"","nick":"艾伦圣玛旗舰店","coupon":""}'

服务器收到如下代码..最后都括号不匹配

{ itemdata: '\'{nType:1,nBaoYou:0,szItemId:536347062406,title:�ﶬ�¿�Ůװ����������ë ��Ů�
Ӻ��������ݳ�����ͷ��֯��������,secondTitle:,imgURL:http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-it
em_pic.jpg,promotionTxt:��ʱ��ɱ��,fOldPrice:108,fThirdPrice:0,fCommissionPer:10.5,fCommission:2.08,nTgCount:0,nSellC
ount:37,fPingFen:0,nPingjiaCount:5,szTgURL:,szWebTgURL:,szBlogTgURL:,shortUrl:,szDesc:,nick:����ʥ���콢��,coupon:}\''
 }

还有如果直接传递json 服务器好像把接受到的又给包装了一层key=>value,下面是curl命令.

curl -X POST http://localhost:3000/api/licenses/eee -d {"itemdata":{"nType":"1","nBaoYou":"0",
"szItemId":"536347062406","title":"秋冬新款女装韩版半高领毛 衣女加厚修身显瘦长袖套头针织衫打底衫","secondTitle":"","i
mgURL":"http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg","promotionTxt":"限时秒杀价","fOl
dPrice":"108","fThirdPrice":"0","fCommissionPer":"10.5","fCommission":"2.08","nTgCount":"0","nSellCount":"37","fPingF
en":"0","nPingjiaCount":"5","szTgURL":"","szWebTgURL":"","szBlogTgURL":"","shortUrl":"","szDesc":"","nick":"艾伦圣玛
旗舰店","coupon":""}}

下面是结果

{
  "{itemdata:{nType:1,nBaoYou:0,szItemId:536347062406,title:锟斤冬锟铰匡拷女装锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷毛 锟斤拷
女锟接猴拷锟斤拷锟斤拷锟斤拷锟捷筹拷锟斤拷锟斤拷头锟斤拷织锟斤拷锟斤拷锟斤拷锟斤拷,secondTitle:,imgURL:http://img4.tb
cdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg,promotionTxt:锟斤拷时锟斤拷杀锟斤拷,fOldPrice:108,fThir
dPrice:0,fCommissionPer:10.5,fCommission:2.08,nTgCount:0,nSellCount:37,fPingFen:0,nPingjiaCount:5,szTgURL:,szWebTgURL
:,szBlogTgURL:,shortUrl:,szDesc:,nick:锟斤拷锟斤拷圣锟斤拷锟届舰锟斤拷,coupon:}}": ""
}

现在是两个问题.
1第一个到底应该怎么在curl 里传递中文json?
2如何使服务器解析正确的json ,因为用js的http.post 是可以正确接受的....

希望大神给予指导

回复内容:

CURL 命令这么写的.

curl -X POST http://localhost:3000/api/licenses/eee -d itemdata='{"nType":"1","nBaoYou":"0","szItemId":"536347062406","title":"秋冬新款女装韩版半高领毛 衣女加厚修身显瘦长袖套头针织衫打底衫","secondTitle":"","imgURL":"http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg","promotionTxt":"限时秒杀价","fOldPrice":"108","fThirdPrice":"0","fCommissionPer":"10.5","fCommission":"2.08","nTgCount":"0","nSellCount":"37","fPingFen":"0","nPingjiaCount":"5","szTgURL":"","szWebTgURL":"","szBlogTgURL":"","shortUrl":"","szDesc":"","nick":"艾伦圣玛旗舰店","coupon":""}'

服务器收到如下代码..最后都括号不匹配

{ itemdata: '\'{nType:1,nBaoYou:0,szItemId:536347062406,title:�ﶬ�¿�Ůװ����������ë ��Ů�
Ӻ��������ݳ�����ͷ��֯��������,secondTitle:,imgURL:http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-it
em_pic.jpg,promotionTxt:��ʱ��ɱ��,fOldPrice:108,fThirdPrice:0,fCommissionPer:10.5,fCommission:2.08,nTgCount:0,nSellC
ount:37,fPingFen:0,nPingjiaCount:5,szTgURL:,szWebTgURL:,szBlogTgURL:,shortUrl:,szDesc:,nick:����ʥ���콢��,coupon:}\''
 }

还有如果直接传递json 服务器好像把接受到的又给包装了一层key=>value,下面是curl命令.

curl -X POST http://localhost:3000/api/licenses/eee -d {"itemdata":{"nType":"1","nBaoYou":"0",
"szItemId":"536347062406","title":"秋冬新款女装韩版半高领毛 衣女加厚修身显瘦长袖套头针织衫打底衫","secondTitle":"","i
mgURL":"http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg","promotionTxt":"限时秒杀价","fOl
dPrice":"108","fThirdPrice":"0","fCommissionPer":"10.5","fCommission":"2.08","nTgCount":"0","nSellCount":"37","fPingF
en":"0","nPingjiaCount":"5","szTgURL":"","szWebTgURL":"","szBlogTgURL":"","shortUrl":"","szDesc":"","nick":"艾伦圣玛
旗舰店","coupon":""}}

下面是结果

{
  "{itemdata:{nType:1,nBaoYou:0,szItemId:536347062406,title:锟斤冬锟铰匡拷女装锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷毛 锟斤拷
女锟接猴拷锟斤拷锟斤拷锟斤拷锟捷筹拷锟斤拷锟斤拷头锟斤拷织锟斤拷锟斤拷锟斤拷锟斤拷,secondTitle:,imgURL:http://img4.tb
cdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg,promotionTxt:锟斤拷时锟斤拷杀锟斤拷,fOldPrice:108,fThir
dPrice:0,fCommissionPer:10.5,fCommission:2.08,nTgCount:0,nSellCount:37,fPingFen:0,nPingjiaCount:5,szTgURL:,szWebTgURL
:,szBlogTgURL:,shortUrl:,szDesc:,nick:锟斤拷锟斤拷圣锟斤拷锟届舰锟斤拷,coupon:}}": ""
}

现在是两个问题.
1第一个到底应该怎么在curl 里传递中文json?
2如何使服务器解析正确的json ,因为用js的http.post 是可以正确接受的....

希望大神给予指导

你是想做测试吧,去下载一个postman吧

试试把要POST的数据保存在json文件中,例如 itemdata.json:

{
    "nType":"1",
    "nBaoYou":"0",
    "szItemId":"536347062406",
    "title":"秋冬新款女装韩版半高领毛 衣女加厚修身显瘦长袖套头针织衫打底衫",
    "secondTitle":"",
    "imgURL":"http://img4.tbcdn.cn/tfscom/i1/TB1OniJLXXXXXXzXpXXXXXXXXXX_!!0-item_pic.jpg",
    "promotionTxt":"限时秒杀价",
    "fOldPrice":"108",
    "fThirdPrice":"0",
    "fCommissionPer":"10.5",
    "fCommission":"2.08",
    "nTgCount":"0",
    "nSellCount":"37",
    "fPingFen":"0",
    "nPingjiaCount":"5",
    "szTgURL":"",
    "szWebTgURL":"",
    "szBlogTgURL":"",
    "shortUrl":"",
    "szDesc":"",
    "nick":"艾伦圣玛旗舰店",
    "coupon":""
}

Headers中设置Content-Typeapplication/json然后执行以下命令:

curl http://localhost:3000/api/licenses/eee -H "Content-Type: application/json" -X POST --data @itemdata.json

可以对数据编码后传输。

用firefox看看人家传的什么,用的什么header,照准了填上去,我就这么干

要保证shell和php的编码一致