Promise-based Wrapper for Simple HTTP Requests built on top of Bluebird and Request
firsturl = 'someurl.json';
secondurl = 'some_second_url.json';
request = require('request-promise');
options = {
url: firsturl,
json: false,
header: "some_username_password"
}
request(options)
.then(function(data1) {
var data_first = JSON.parse(data1); //results from the first url
options.url = secondurl;
request(options)
.then(function(data2) {
var data_second = JSON.parse(data2);
console.log(data_second);
//why it prints the results from the first url???
})
})
await rp(…)
you could use await rp(…).promise()
. Since rp(…)
returns a mixed object that is a certain request object and a promise at the same time that may confuse the runtime. .promise()
returns just the promise and that should definitely work with await
.
So my code looks like this
return rp(options)
.then(response => {
let timeEntries = JSON.parse(response)['time-entries'];
return timeEntries;
})
.catch(err => console.log(err.MESSAGE));
Which is a part of a method.
I am getting this error Unhandled rejection Error: StatusCodeError: 400 - "{\"MESSAGE\":\"Invalid Parameters - 'page' must be greater or equal to 1\",\"STATUS\":\"Error\"}"
then
and catch
in the snippet is perfect. The only explanation I currently have for your unhandled rejection is that you actual code doesn’t match the snippet. Or the rejection came from a different request at another spot in your code. Could you double check?
timeout
option. For the response time you can build a wrapper around your requests.
0.3.2
of request-promise and I'm seeing some issues where the request hangs indefinitely after a POST ... so I want to go through each version's release notes and find out which one might have fixed this issue or incorporated an update of request
to fix such symptoms
import request from 'request-promise-native';
TypeError: global.XMLHttpRequest is not a constructor
multipart/form-data