无法将带有自定义 headers 的 HTTP POST 发送到外部服务器
Can't send HTTP POST with custom headers to external server
我目前正在使用 AWS Lambda 开发一项 Alexa 技能,除一件事外,一切都运行良好。我似乎无法成功 POST HTTP 参数/自定义 headers 到我的服务器。它可以完美地抓取信息,但我不明白为什么它不发送参数/自定义 headers.
我发送 HTTP POST 请求的代码如下所示:
function httpGetMall(latitude, longitude, callback) {
var options = {
host: 'myserver.com',
path: '/path/to/script.php',
auth: 'myAuthenticationPassword'.toString('base64'),
method: 'POST',
headers: {'latitude': latitude.toString(), 'longitude': longitude.toString()}
};
var req = http.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
callback(body);
});
});
req.end();
req.on('error', (e) => {
});
}
我知道函数被正确调用了,因为它 returns 回调中的数据非常完美。
在我的 php 脚本中,我试图像这样获取值:
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
我尝试在函数中手动设置纬度和经度以查看它们是否没有被传入,但服务器仍然没有收到它们。
如有任何帮助,我们将不胜感激。谢谢!
POST 数据未在 headers 中发送。它在 body 中作为编码字符串发送。在您的代码中,您将对其进行正确编码,然后使用 req.write()
发送它。 nodejs doc for http.request()
.
中有一个 POST 代码示例
以下是修改代码以正确执行此操作的方法:
var querystring = require('querystring');
function httpGetMall(latitude, longitude, callback) {
var options = {
host: 'myserver.com',
path: '/path/to/script.php',
auth: 'myAuthenticationPassword'.toString('base64'),
method: 'POST',
};
var req = http.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
callback(null, body);
});
});
req.on('error', (e) => {
callback(e);
});
// format the data appropriately for the POST body
var postData = querystring.stringify({latitude: latitude, longitude: longitude});
// write the POST body
req.write(postData);
req.end();
}
注意:您还需要添加适当的错误处理。可能您应该将回调变成典型的 nodejs 异步回调,该回调将错误作为第一个参数,将数据作为第二个参数。然后,您可以在收到错误时调用 callback(err)
,在收到响应数据时调用 callback(null, body)
。我修改了我的答案以显示这一点。
我目前正在使用 AWS Lambda 开发一项 Alexa 技能,除一件事外,一切都运行良好。我似乎无法成功 POST HTTP 参数/自定义 headers 到我的服务器。它可以完美地抓取信息,但我不明白为什么它不发送参数/自定义 headers.
我发送 HTTP POST 请求的代码如下所示:
function httpGetMall(latitude, longitude, callback) {
var options = {
host: 'myserver.com',
path: '/path/to/script.php',
auth: 'myAuthenticationPassword'.toString('base64'),
method: 'POST',
headers: {'latitude': latitude.toString(), 'longitude': longitude.toString()}
};
var req = http.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
callback(body);
});
});
req.end();
req.on('error', (e) => {
});
}
我知道函数被正确调用了,因为它 returns 回调中的数据非常完美。
在我的 php 脚本中,我试图像这样获取值:
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
我尝试在函数中手动设置纬度和经度以查看它们是否没有被传入,但服务器仍然没有收到它们。
如有任何帮助,我们将不胜感激。谢谢!
POST 数据未在 headers 中发送。它在 body 中作为编码字符串发送。在您的代码中,您将对其进行正确编码,然后使用 req.write()
发送它。 nodejs doc for http.request()
.
以下是修改代码以正确执行此操作的方法:
var querystring = require('querystring');
function httpGetMall(latitude, longitude, callback) {
var options = {
host: 'myserver.com',
path: '/path/to/script.php',
auth: 'myAuthenticationPassword'.toString('base64'),
method: 'POST',
};
var req = http.request(options, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
callback(null, body);
});
});
req.on('error', (e) => {
callback(e);
});
// format the data appropriately for the POST body
var postData = querystring.stringify({latitude: latitude, longitude: longitude});
// write the POST body
req.write(postData);
req.end();
}
注意:您还需要添加适当的错误处理。可能您应该将回调变成典型的 nodejs 异步回调,该回调将错误作为第一个参数,将数据作为第二个参数。然后,您可以在收到错误时调用 callback(err)
,在收到响应数据时调用 callback(null, body)
。我修改了我的答案以显示这一点。