dsbj66959 2017-03-11 15:41
浏览 62
已采纳

无法将带有自定义标头的HTTP POST发送到外部服务器

I am currently working on an Alexa skill using AWS Lambda, and all has been working perfectly except for one thing. I can't seem to successfully POST HTTP param's / custom headers to my server. It can grab pull the information perfectly, but I cannot figure out why it is not sending the parameters / custom headers.

My code for sending a HTTP POST request looks like this :

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) => {

    });
}

I know for a fact the function is being called correctly, as it returns the data on callback perfectly.

On my php script, I am attempting to grab the values like so :

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];

I have tried manually setting the latitude and longitude inside the function to see if they were not getting passed in, but the server still never received them.

Any help would be greatly appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • doucang2871 2017-03-11 15:52
    关注

    POST data is not sent in headers. It is sent as an encoded string in the body. In your code, you would encode it properly and then send it with req.write(). There's a POST code example in the nodejs doc for http.request().

    Here's how you could modify your code to do it properly:

    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();
    }
    

    Note: You also need to add proper error handling. Probably you should make your callback into a typical nodejs async callback that takes an error as the first argument and data as the second. Then, you can just call callback(err) when you get an error and callback(null, body) when you get the response data. I've modified my answer to show this also.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码