Home  >  Article  >  Backend Development  >  javascript - How to use curl to post a text containing Chinese json to the server?

javascript - How to use curl to post a text containing Chinese json to the server?

WBOY
WBOYOriginal
2016-08-10 09:07:241156browse

CURL command is written like this.

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

The server received the following code.. The brackets at the end do not match

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

Also, if you pass json directly, the server seems to wrap it with a layer of key=>value. Here is the curl command.

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

Here are the results

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

Now there are two questions.
1. How to pass Chinese json in curl?
2. How to make the server parse the correct json, because http.post using js can be accepted correctly....

Hope God can give you guidance

Reply content:

CURL command is written like this.

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

The server received the following code.. The brackets at the end do not match

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

Also, if you pass json directly, the server seems to wrap it with a layer of key=>value. Here is the curl command.

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

Here are the results

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

Now there are two questions.
1. How to pass Chinese json in curl?
2. How to make the server parse the correct json, because http.post using js can be accepted correctly....

Hope God can give guidance

If you want to do testing, download a postman

Try saving the data to POST in a json file, for example 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>

Set Content-Type in Headers to application/json and then execute the following command:

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

The data can be encoded and transmitted.

Use firefox to see what others have posted, what headers are used, and fill it in if it’s correct, that’s what I’ll do

Make sure the encoding of shell and php are consistent

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn