2017-05-08 92 views
0

我試圖讓雙簧管發送一些數據與請求,但它似乎並沒有工作。這是我簡單的測試腳本,我也包括其中工程罰款request.js例如:NodeJS雙簧管不發送請求正文到PHP服務器

// Doesn't work 
var oboe = require('oboe'); 
oboe({ 
    method: 'POST', 
    url: 'http://localhost:8440/oboe.php', 
    body: JSON.stringify({ 
     foo: 'bar', 
    }), 
}).done(function(data) { 
    console.log('oboe', data); 
}); 

// Works 
var request = require('request'); 
request({ 
    json: true, 
    method: 'POST', 
    url: 'http://localhost:8440/oboe.php', 
    body: JSON.stringify({ 
     foo: 'bar', 
    }), 
}, function(error, response, body) { 
    console.log('request', body); 
}); 

此輸出:

$ node test.js 
oboe { get: [], post: [], body: '' } 
request { get: [], post: [], body: '"{\\"foo\\":\\"bar\\"}"' } 

以及測試我的簡單的PHP文件:

<?php 
die(json_encode([ 
    'get' => $_GET, 
    'post' => $_POST, 
    'body' => file_get_contents('php://input'), 
])); 

我敢肯定,我正在做一些簡單的錯誤,但無法弄清楚什麼。

回答

0

好吧,我想我明白了。似乎需要發送Content-Length標題。

var data = JSON.stringify({ 
    foo: 'bar', 
}); 
oboe({ 
    method: 'POST', 
    url: 'http://localhost:8440/oboe.php', 
    body: data, 
    headers: { 
     'Content-Type': 'application/json', 
     'Content-Length': data.length, 
    }, 
}).done(function(data) { 
    console.log('oboe', data); 
});