How to make a Curl call in node.js with arguments

馋奶兔 提交于 2019-12-11 04:35:39

问题


I am trying to make this curl request from a node module. not sure how to pass the arguments.

curl -u myusername https://myusername.cloudant.com/_api/v2/monitoring/disk_use?cluster=myclustername&format=json

so far I got the following code. I am using node-libcurl in hopes that it would be easy.

    var Curl = require( 'node-libcurl' );
    var curl = new Curl();


    curl.setOpt( 'URL', 'myusername.cloudant.com/_api/v2/monitoring/disk_use?cluster=myclustername&format=json' );

curl.on( 'end', function( statusCode, body, headers ) {

        console.info( statusCode );
        console.info( '---' );
        console.info( body );
        console.info( '---' );
        console.info( this.getInfo( 'TOTAL_TIME' ) );

        this.close();
    });

So I don't know how to pass the -u flag in the command. Thanks in advance any direction would be helpful.


回答1:


You can set username and password by:

curl.setOpt( Curl.option.USERNAME, 'username' );
curl.setOpt( Curl.option.PASSWORD, 'password' );

Reference: https://github.com/JCMais/node-libcurl/blob/develop/examples/email-retrieve.js




回答2:


Check out the curl examples

curl.setOpt( Curl.option.USERNAME, 'username' );
curl.setOpt( Curl.option.PASSWORD, 'password' );



回答3:


You can achieve this using request package from NPM. check HTTP Authentication section on Github.



来源:https://stackoverflow.com/questions/38400497/how-to-make-a-curl-call-in-node-js-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!