简体   繁体   中英

Invalid protocol: undefined - NodeJS error on POST request -

I'm getting this error coming from my require.post(error)
its a lambda function deployed from a vagrant box. It is a wrapper for an api, the event.body has the properly formatted post json and everything is working perfectly when the post is done from postman. I have seen a roughly similar problem solved by

npm config set proxy http://usr:pwd@host:port
npm config set https-proxy http://usr:pwd@host:port

Please help! : )

Error: Invalid protocol: undefined
at Request.init (/var/task/node_modules/request/request.js:454:31)
at new Request (/var/task/node_modules/request/request.js:127:8)
at request (/var/task/node_modules/request/index.js:53:10)
at Function.post (/var/task/node_modules/request/index.js:61:12)
at module.exports.startVerifyProcess (/var/task/handler.js:83:13)

my Code:

module.exports.startVerifyProcess = (event, context, callback) => {
    var body = JSON.parse(event.body);
    const params = querystring.parse(event.body);
console.warn(body);
var Re;

    var post_options = {
        host: 'api.demo.veri.com',
        path: '/api/v1/verify/requests',
        port: 443,
        method: 'POST',
        headers: {
           // 'Content-Type': 'application/json',
           // 'Content-Length': Buffer.byteLength(event.body),
            "Authorization": "myValidAuth",
        },           
    }

    request.post(post_options, body, function(err, res, resBody) {
            if (err){
                console.error(err);
            }

        console.warn("RESPONSE "  + resBody);
        });

    callback(null, {
        statusCode: 200,
        body: JSON.stringify({
            body: Re
        })
    });
}

The issue here is that request should not take the body as the second parameter. request.post(post_options, function(err, res, resBody) is correct and the body should be in the post_options object. While your at it chose either camelCase or snake_case for everything in that project. Also check out node-fetch I would consider it a solid upgrade from using request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM