最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replacing node request.post with request-promise - Stack Overflow

programmeradmin0浏览0评论

I have: I used Node.js request module to get authorization token:

Working code without promise

var request = require('request');
var querystring = require('querystring');

var requestOpts = querystring.stringify({
    client_id: 'Subtitles',
    client_secret: 'X............................s=',
    scope: '',
    grant_type: 'client_credentials'
});

request.post({
    encoding: 'utf8',
    url: "",
    body: requestOpts
}, function(err, res, body) { //CALLBACK FUNCTION
    var token = JSON.parse(body).access_token;
    amkeAsyncCall(token);
});

I want: It takes some time to get that token. In turn I need makeAsyncCall from getToken callback. So I decide to use a request-promise from here.

Problem: request-promise seems don't work for me at all.

The same (not working) code with promise:

    var rp = require('request-promise');
    var querystring = require('querystring');

    var requestOpts = {
        encoding: 'utf8',
        uri: '',
        method: 'POST',
        body: querystring.stringify({
            client_id: 'Subtitles',
            client_secret: 'Xv2Oae6Vki4CnYcSF1SxSSBtO1x4rX47zhLUE/OqVds=',
            scope: '',
            grant_type: 'client_credentials'
        })
    };

    rp(requestOpts)
    .then(function() {
        console.log(console.dir);
    })
    .catch(function() {
        console.log(console.dir);
    });

I have: I used Node.js request module to get authorization token:

Working code without promise

var request = require('request');
var querystring = require('querystring');

var requestOpts = querystring.stringify({
    client_id: 'Subtitles',
    client_secret: 'X............................s=',
    scope: 'http://api.microsofttranslator.',
    grant_type: 'client_credentials'
});

request.post({
    encoding: 'utf8',
    url: "https://datamarket.accesscontrol.windows/v2/OAuth2-13",
    body: requestOpts
}, function(err, res, body) { //CALLBACK FUNCTION
    var token = JSON.parse(body).access_token;
    amkeAsyncCall(token);
});

I want: It takes some time to get that token. In turn I need makeAsyncCall from getToken callback. So I decide to use a request-promise from here.

Problem: request-promise seems don't work for me at all.

The same (not working) code with promise:

    var rp = require('request-promise');
    var querystring = require('querystring');

    var requestOpts = {
        encoding: 'utf8',
        uri: 'https://datamarket.accesscontrol.windows/v2/OAuth2-13',
        method: 'POST',
        body: querystring.stringify({
            client_id: 'Subtitles',
            client_secret: 'Xv2Oae6Vki4CnYcSF1SxSSBtO1x4rX47zhLUE/OqVds=',
            scope: 'http://api.microsofttranslator.',
            grant_type: 'client_credentials'
        })
    };

    rp(requestOpts)
    .then(function() {
        console.log(console.dir);
    })
    .catch(function() {
        console.log(console.dir);
    });
Share Improve this question asked Sep 24, 2014 at 9:45 VB_VB_ 45.7k44 gold badges160 silver badges311 bronze badges 1
  • 2 Any promise library would do, you don't need a whole package for that. Just use bluebird and do Promise.promisify(require("request")) and it'd work – Benjamin Gruenbaum Commented Sep 24, 2014 at 10:07
Add a ment  | 

4 Answers 4

Reset to default 1

I tested your code with the latest version of Request-Promise (0.3.1) and it works fine.

Just the last part of logging to the console was not correct. Use:

rp(requestOpts)
    .then(function(body) {
        console.dir(body);
    })
    .catch(function(reason) {
        console.dir(reason);
    });

You could set the json attribute in requestOpts be true, it works for me.

var rp = require('request-promise');
var querystring = require('querystring');
var requestOpts = {
    encoding: 'utf8',
    uri: 'https://datamarket.accesscontrol.windows/v2/OAuth2-13',
    method: 'POST',
    json: true,
    body:    {
        client_id: 'Subtitles',
        client_secret: 'Xv2Oae6Vki4CnYcSF1SxSSBtO1x4rX47zhLUE/OqVds=',
        scope: 'http://api.microsofttranslator.',
        grant_type: 'client_credentials'
    }
};

rp(requestOpts)
.then(function() {
    console.log(console.dir);
})
.catch(function() {
    console.log(console.dir);
});

I use the node.js package "unirest".

var unirest = require('unirest');
var dataObj = {};
var Request = unirest.post('http://127.0.0.1:' + port + '/test/4711DE/de');
Request.headers({ 'Accept': 'application/json' })                    
 .type('json')
 .send(JSON.stringify(dataObj))
 .auth({
   user: 'USERNAME',
   pass: 'PASSWORD',
   sendImmediately: true
 })
 .end(function (response) {
   assert.equal(200, response.statusCode);
   // ...
 });

I had the same issue I just added this property headers = { 'Content-Type': 'application/json' };

var requestOpts = {
    encoding: 'utf8',
    uri: 'https://datamarket.accesscontrol.windows/v2/OAuth2-13',
    method: 'POST',
    headers = { 'Content-Type': 'application/json' };
    body: JSON.stringify({
        client_id: 'Subtitles',
        client_secret: 'Xv2Oae6Vki4CnYcSF1SxSSBtO1x4rX47zhLUE/OqVds=',
        scope: 'http://api.microsofttranslator.',
        grant_type: 'client_credentials'
    })
};
发布评论

评论列表(0)

  1. 暂无评论