Reputation: 61
The chain I have is retrieving information from a Sql Server and then setting up the data to then be sent to an api via post. The problem is i receive this RequestError message, **"TypeError [ERR_INVALID_HTTP_TOKEN]: Header name must be a valid HTTP token ["key"]"
Objective
-Retrieve Data using id
-Format data
-Send new, separate request to an api with formatted data
-Resolve results returned from api call
router.js
router.get('/request/:id', controller.data_post)
module.exports = router
Controller.js
exports.data_post = function(req, res) {
...
RetrieveData( req.id ) //retrieves data
.then( results => { //format data
var obj = formatData(results);
let body = [];
body.push(obj);
return body //resolve formatted data
} //End Of Promise
})
.then( body => { //Send new, separate request to an api with formatted data
var options = :{
method: 'POST',
uri: 'url',
headers: {
'key':'value',
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
json:true
}
return option
})
.then( results => {
//send results
})
.catch( error => {
//error routine
})
}
RetrieveData.js
function RetrieveData( id ){
const promise = new Promise((resolve, reject) => {
...
resolve(data)
}
return promise;
}
RequestUtility.js
const request = require('request-promise')
function requestutility(options) {
return request(options)
.then( response => {
return response;
})
.catch( error => {
return error;
})
}
Current Error
callback: function RP$callback(err, response, body) {
Upvotes: 0
Views: 4010
Reputation: 7770
Couple of problems I see here
You don't need to return Promise.resolve and Promise.reject in the request utility method. Since request promise returns a promise, your promise will be resolved when succesfull and rejected when error. So you can get rid of requestutility alltogether.
You are wrapping results in new Promise
which is not required.
resolve( requestutility(option))
doesn't work the way you are expecting it to work as it will resolve to a promise instead of value.const request = require("request-promise");
RetrieveData(id)
.then(results => {
const obj = formatData(results);
const body = [];
body.push(obj);
return body;
})
.then(body => {
const options = {
"method": "POST",
"uri": "url",
"headers": {
"key": "value",
"Content-Type": "application/json"
},
"body": JSON.stringify(body),
"json": true
};
return request(options);
})
.then(results => {
// send results
})
.catch(error => {
// error routine
});
Upvotes: 1