Reputation: 511
I get this error when I try to run my node js application:
_http_client.js:127 throw new TypeError('Request path contains unescaped characters'); ^
TypeError: Request path contains unescaped characters at new ClientRequest (_http_client.js:127:13) at Object.request (http.js:38:10) at Object.exports.getDuration (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:53:20) at /Users/paulcarron/git/integration-test-runner/modules/jobRunningService.js:75:21 at IncomingMessage. (/Users/paulcarron/git/integration-test-runner/modules/getEventLog.js:35:13) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1056:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)
After some investigation I've concluded its because of the + myId
in getEventLog.js(see code at the very bottom of question). The entire script is called by this function in a different script:
child.stderr.on('data', function(uuid, data) {
getEventLog.getId(uuid, function(err, id){
if(err) return console.log(err)
getEventLog.getDuration(id, function(err, duration){
if(err) return console.log(err)
jobLogger.info(escape(data) + "<br />");
})
});
});
I thought there might have been a whitespace in myId so I added a console.log("myId: " + myId)
and found that it returned myId: [object Object]
. How do I fix this so that myId
is an ID in a correct format?
var http = require("http");
var fs = require('fs');
var async = require('async');
var readline = require('readline')
//var db = require('./dbPool');
//get file name
var options = {
"method" : "GET",
"hostname" : "127.0.0.1",
"port" : "18080",
"path" : "/api/v1/applications/"
};
exports.getId = function (uuid, callback) {
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = JSON.parse(Buffer.concat(chunks));
var arrFound = Object.keys(body).filter(function(key) {
if (body[key].name.indexOf(uuid) > -1) {
return body[key].name;
}
}).reduce(function(obj, key){
obj = body[key].id;
return obj;
}, {});
callback(null, arrFound);
});
});
req.end();
}
exports.getDuration = function (myId, callback) {
//getEventLog.getId(function(err, id){
//get file name
var options = {
"method" : "GET",
"hostname" : "127.0.0.1",
"port" : "18080",
"path" : "/api/v1/applications/" + myId
};
var req = http.request(options, function (res) {
console.log(options)
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = JSON.parse(Buffer.concat(chunks));
var attempts = body.attempts
var arrFound = Object.keys(body).filter(function(key) {
return attempts[0].duration;
}).reduce(function(obj, key){
obj = body.attempts[0].duration;
return obj;
}, {});
//console.log(arrFound);
callback(null, arrFound);
});
});
req.end();
// })
};
Upvotes: 0
Views: 1870
Reputation: 511
The problem here was that myId was a JSON object rather than a string. This meant that a html page was returned tather than the JSON location I required.
Upvotes: 1