简体   繁体   English

无效的协议:未定义-POST请求上的NodeJS错误-

[英]Invalid protocol: undefined - NodeJS error on POST request -

I'm getting this error coming from my require.post(error) 我收到此错误来自我的require.post(错误)
its a lambda function deployed from a vagrant box. 其从无业游民的盒子部署的lambda函数。 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. 它是一个api的包装,event.body具有正确格式的post json,当postman完成post时,一切工作正常。 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. request.post(post_options, function(err, res, resBody)是正确的,主体应该在post_options对象中。当您为该项目中的所有内容选择了camelCase或snake_case时,也请查看节点获取它是使用请求的可靠升级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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