需要帮助才能将curl转换为请求

时间:2015-08-18 12:43:15

标签: node.js http curl express request

我有这样的卷发 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 param的问题。如何在http req

中使用它

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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