Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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
});
like image 500
Hlib Derbenov Avatar asked Jan 03 '19 15:01

Hlib Derbenov


Video Answer


3 Answers

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

like image 134
Mostafa Ghadimi Avatar answered Oct 19 '22 09:10

Mostafa Ghadimi


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

like image 2
Luiz Fernando da Silva Avatar answered Oct 19 '22 11:10

Luiz Fernando da Silva


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
    });
}
like image 1
Hlib Derbenov Avatar answered Oct 19 '22 09:10

Hlib Derbenov