Rumah  >  Artikel  >  pembangunan bahagian belakang  >  javascript - 如何用curl post 一段包含中文json的文本到服务器?

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

WBOY
WBOYasal
2016-08-10 09:07:241156semak imbas

CURL 命令这么写的.

<code>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":""}'
</code>

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

<code>{ 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:}\''
 }
</code>

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

<code>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":""}}
</code>

下面是结果

<code>{
  "{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:}}": ""
}
</code>

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

希望大神给予指导

回复内容:

CURL 命令这么写的.

<code>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":""}'
</code>

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

<code>{ 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:}\''
 }
</code>

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

<code>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":""}}
</code>

下面是结果

<code>{
  "{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:}}": ""
}
</code>

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

希望大神给予指导

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

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

<code class="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":""
}</code>

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

<code class="bash">curl http://localhost:3000/api/licenses/eee -H "Content-Type: application/json" -X POST --data @itemdata.json</code>

可以对数据编码后传输。

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

要保证shell和php的编码一致

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn