如何使用参数在node.js中进行Curl调用 [英] How to make a Curl call in node.js with arguments

查看:777
本文介绍了如何使用参数在node.js中进行Curl调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从节点模块发出卷曲请求。不确定如何传递参数。

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

到目前为止,我已经获得了以下代码。我正在使用 node-libcurl 希望它会很容易。

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();
    });

所以我不知道如何在命令中传递-u标志。在此先谢谢您的指导。

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

推荐答案

您可以通过以下方式设置用户名和密码:

You can set username and password by:

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

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

这篇关于如何使用参数在node.js中进行Curl调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆