【问题标题】:NodeJS 12.x https version of a curl request on AWS LambdaAWS Lambda 上的 curl 请求的 NodeJS 12.x https 版本
【发布时间】:2020-06-08 05:49:40
【问题描述】:
    curl -d "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f" -X POST https://sandbox.somedomain.co.za/what/something/validate

我的尝试:

const https = require("https");
const querystring = "m_payment_id=m_payment_id-xxxxxxxxxxxxxxxxxyyy&pf_payment_id=990396&payment_status=COMPLETE&item_name=Subscription&item_description=Monthly+Subscription&amount_gross=99.00&amount_fee=-6.74&amount_net=92.26&custom_str1=&custom_str2=&custom_str3=&custom_str4=&custom_str5=&custom_int1=&custom_int2=&custom_int3=&custom_int4=&custom_int5=&name_first=&name_last=&email_address=christo%40g4-ape.co.za&merchant_id=0000000&token=0000000-0000-0000-3a83-25bc733a307b&billing_date=2020-02-21&signature=3895d0769b56862b842da5067af4483f";

return new Promise((resolve, reject) => {
        const options = {
            hostname: 'sandbox.somedomain.co.za',
            port: 443,
            path: 'what/something/validate',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': Buffer.byteLength(querystring)
            }
        };

        const req = https.request(options, (res) => {
            console.log('statusCode: ' + res.statusCode);
            console.log('headers: ' + JSON.stringify(res.headers));
            res.setEncoding('utf8');

            let data = '';
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on('end', () => {
                console.log('BODY: ' + data);
            });

            resolve('Success');
        });

        req.on('error', (e) => {
            console.log('problem with request: ' + e.message);
            reject(e.message);
        });

        // write data to request body
        req.write(querystring);
        req.end();
    });

我在 NodeJS 代码上不断收到 statusCode 400,curl 工作正常。为了安全起见,主机名 hs 显然已更改。

有人可以告诉我我在这里做错了什么吗?

【问题讨论】:

    标签: node.js curl https aws-lambda


    【解决方案1】:

    在发帖时发送正文时需要 Content-Length。在您的情况下,所有信息都作为查询字符串参数传递,因此您可以摆脱所有标题。

    另外,您应该在 res.on('end' 内部解析。你这样做的方式将在函数完成执行之前完成。

    【讨论】:

    • 我完全删除了标题并移动了 resolve('Success') res.on('end', () => { console.log('BODY: ' + data); resolve('Success'); }); 但它仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2018-05-02
    • 2020-10-06
    • 1970-01-01
    • 2020-06-08
    • 2021-08-10
    • 2018-06-04
    相关资源
    最近更新 更多