我试图让双簧管用请求发送一些数据,但它似乎没有用。这是我的简单测试脚本,我还包含了一个可行的request.js示例:
// 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 :(得分:1)
好的,我想我已经明白了。似乎需要发送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);
});