2014-10-10 162 views
0

這是一個簡單的我猜: 我想請求一個Node.js服務器,因爲我目前正在使用cURL。無論我嘗試什麼,我都會從服務器收到錯誤404。使用請求轉換節點js的cURL請求

這裏是我的合法捲曲命令,它會返回一個JavaScript對象:

curl --data "ajax=1&example=test" http://some-site-example.com 

閱讀捲曲說明書及申請手冊後,這裏是我在Node.js嘗試:

request.post({'content-type':'application/x-www-form-urlencoded',ajax:'1',example:'test',url:'http://some-site-example.com'}, function(err, result, body) { 
    if (err) console.log(err);  
    else console.log(body); 
}); 
+0

是[要求](HTTPS: //www.npmjs.org/package/request)模塊? – Ravi 2014-10-10 12:19:28

回答

1

你的選擇是錯誤的爲application/x-www-form-urlencoded form。另外,通過指定form,它會自動設置正確的Content-Type。所以,試試這個:

request.post({form: {ajax:'1', example:'test'}, url: 'http://some-site-example.com'}, function(err, response, body) { 

代替:

request.post({'content-type':'application/x-www-form-urlencoded',ajax:'1',example:'test',url:'http://some-site-example.com'}, function(err, result, body) { 
+0

謝謝你的解答和鏈接 – Thook 2014-10-10 12:44:41

0

這應該這樣做,形式自動將內容類型,here's the manual

request.post('http://some-site-example.com', {form: {ajax:'1',example:'test'}}, function(err, result, body) { 
    if (err) console.log(err);  
    else console.log(body); 
}); 
+0

謝謝你的回答 – Thook 2014-10-10 12:45:19