2017-05-08 86 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); 
});