Hlib Derbenov
Hlib Derbenov

Reputation: 939

TypeError [ERR_UNESCAPED_CHARACTERS] Request path contains unescaped characters

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:

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
});

Upvotes: 27

Views: 37805

Answers (3)

Luiz Fernando da Silva
Luiz Fernando da Silva

Reputation: 1499

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

Upvotes: 3

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6766

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

Upvotes: 42

Hlib Derbenov
Hlib Derbenov

Reputation: 939

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
    });
}

Upvotes: 1

Related Questions