问题
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