27

I am working on Ubuntu with incoming HTTP request from the following URL:

http://<MY-IP>:3000/v1/projects/list

Description: The problem is that I get the following error in terminal:

TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
at new ClientRequest (_http_client.js:127:13)
at Object.request (https.js:300:10)
at Request.start (/home/dev/grem-api-dev/apiv3/node_modules/request/request.js:751:32)
at Request.end (/home/dev/grem-api-dev/apiv3/node_modules/request/request.js:1512:10)
at end (/home/dev/grem-api-dev/apiv3/node_modules/request/request.js:564:14)
at Immediate._onImmediate (/home/dev/grem-api-dev/apiv3/node_modules/request/request.js:578:7)
at processImmediate(timers.js:632:19)

After this error node process disappears from terminal (but still working and API response is sent properly), though I can't see whether it is working or not (image attached). So the only way to interact with node process is to do something like ps aux | grep node or ps Tand manually kill process.

Assuming the error meaning I've found an appropriate code fragment where the error appears (request.js:751:32). Here it is:

  try {
    self.req = self.httpModule.request(reqOptions)
  } catch (err) {
    self.emit('error', err)
    return
  }

The only solution I've come to is to comment self.emit('error', err) code line, which is obviously far from best practice.

The fact is the same code works on other computers (Ubuntu, Windows) with same components versions and no error occurs. Also API endpoints like http://myIp:3000/v1/community/list work fine on all devices.

Here's my component versions:

  • npm — 6.5.0,

  • node — 11.4.0,

  • request — 2.88.0,

  • Ubuntu — 16.04 (Windows — 10)

some code fragments if needed (Express Server creation and specific route in ProjectsController):

const app = express();
app.use('/v1/projects/', ProjectsController);
    
const router = express.Router();
router.post('/list', function(req,res){
   //logic
});
Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
Hlib Derbenov
  • 916
  • 1
  • 7
  • 15

3 Answers3

41

I have used axios and the same problem occured.

My problem was solved using encodeURI() or encodeURIComponent() functions.

const URI = 'example.com';
const encodedURI = encodeURI(URI);

PS: For future reader: use require('url').URL to construct a url object and pass it to node-fetch, which will auto escape url for you.

Useful Links: Link1 | Link2

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
3

For me, I had some IDE issue that injected some invalid char in a endpoint. Once I used encodeURI, it revealed what char was causing the problem. So I re-wrote the endpoint and it worked.

URL: /v2/users/${user.id}/products

What was being read: /v2/users[some invalid char here]/${user.id}/products

1

Solution for this issue was finding code fragment that was sending request to other web resource with URL that contained unescaped characters, and then replacing them according to this article https://www.w3schools.com/tags/ref_urlencode.asp. This fragment was executed by condition, that's why only 1 server with specific OS passed through if/else statement.

const request = require('request');

if (...) {}
else {
    request.get(...) //source of error
    .on('response', function(response) {
        //logic
    });
}
Hlib Derbenov
  • 916
  • 1
  • 7
  • 15
  • 3
    Hey did you tried `encodeURI(url)` details here(https://www.w3schools.com/jsref/jsref_encodeURI.asp) ? – Abhijith S Nov 18 '19 at 05:53
  • @AbhijithS Sure, unescaped characters were properly escaped by this function. Thank you for your attention! – Hlib Derbenov Nov 19 '19 at 15:10
  • Would you please explain more? – Mostafa Ghadimi Mar 11 '20 at 13:06
  • @MostafaGhadimi Well the thing is that you always need to escape special characters (spacebar, &, ? etc) while forming the URI. Otherwise you'll get ERR_UNESCAPED_CHARACTERS error. There's no need to change any npm module's source code. – Hlib Derbenov Mar 12 '20 at 10:06