首頁  >  文章  >  後端開發  >  javascript - 如何用curl post 一段包含中文json的文字到伺服器?

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

WBOY
WBOY原創
2016-08-10 09:07:241156瀏覽

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的編碼一致

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn