【问题标题】:Need help to convert from curl to request需要帮助从 curl 转换为 request
【发布时间】:2015-11-11 09:53:06
【问题描述】:

我有这样的卷发 curl -u 209f734234234j556l45l6j64218ecffb0a900aff998c8e0: -H "Content-Type: application/json" -H "X-Ionic-Application-Id: 123k4j54k" https://push.ionic.io/api/v1/push -d '{"tokens": ["token1,token2,token3"],"notification":{"alert":"NarayaN","android":{"payload":{"$state":"app.home"}}}}'

我已将其转换为这种状态

var request = require('request');

var headers = {
    'Content-Type': 'application/json',
    'X-Ionic-Application-Id': 'e4ea3c2d'
};

var options = {
    url: 'https://push.ionic.io/api/v1/push',
    headers: headers
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

我在使用 -u 参数时遇到问题。如何在 http req 中使用它

通过查看请求,我可以看到 method is post

非常感谢任何帮助。

【问题讨论】:

    标签: node.js http curl express request


    【解决方案1】:

    你可以使用.auth()方法

    request.get('http://some.server.com/').auth('username', 'password', false);
    // or
    request.get('http://some.server.com/', {
      'auth': {
        'user': 'username',
        'pass': 'password',
        'sendImmediately': false
      }
    });
    // or
    request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
    // or
    request.get('http://some.server.com/', {
      'auth': {
        'bearer': 'bearerToken'
      }
    });
    

    https://github.com/request/request#http-authentication

    或者

    https://github.com/request/request#oauth-signing

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2017-06-22
      • 2011-06-01
      • 2016-04-12
      • 1970-01-01
      • 2018-08-05
      • 2018-11-24
      • 2019-12-23
      • 2021-11-06
      相关资源
      最近更新 更多