Search code examples
node.jsmongodbapithemoviedb-api

TypeError: Request path contains unescaped characters, any idea


//route to search (POST http://localhost:8080/api/search)
  apiRoutes.post('/search', function(req, res) {
    console.log('search');
    var query = req.params;
    console.log(query);
    options = {
      protocol : "https:/",
      host: "https://api.themoviedb.org",
      path: "/3/search/movie?api_key=35f7a26be584f96e6b93e68dc3b2eabd&language=en-US&page=1&include_adult=false&query="+query,
    };
    var req = https.request(options, function(res) {
      var chunks = [];
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });
      res.on("end", function () {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
      });
    });
    req.write("{}");
    req.end();
  })

DOES ANYONE KNOW WHERE THE PROBLEM IS?

I'm trying to do a request to do a research to the api the movie db and get the result back


Solution

  • There are some problems with the code. I have tested it and made it to work.

    let options = {
            host: "api.themoviedb.org",
            path: "/3/search/movie?api_key=35f7a26be584f96e6b93e68dc3b2eabd&language=en-US&page=1&include_adult=false&query="+query.data.replace(' ','%20'),
          };
    
    • first of all since you are using https module you don't need to specify the protocol nor you need to put it in the url. That's how your options variable should be.

    • Second you are appending the entire query object to the url which is {} instead you should append a string which will be in one of the key of your query object in my case its query.data

    • Third if there are spaces in the string Eg: Home Alone you to maintain space and avoid the error we replace the string with %20 which is a escaping character.

    • Forth Try giving a unique name for https request variable and its response variable in the callback function or it will override the route's req res variables cause your code to not work. Notice how I have used route's res function to send the data back and end the response

    • Also I am getting the data in req.body and you are using req.params however there are no params defined in your routes. Try going through the documentation for more information

      Here is the complete code

    apiRoutes.post('/search',function (req, res) {
          https = require('https');
          var query = req.body;
          console.log(query.data);
          let options = {
            host: "api.themoviedb.org",
            path: "/3/search/movie?api_key=35f7a26be584f96e6b93e68dc3b2eabd&language=en-US&page=1&include_adult=false&query="+query.data.replace(' ','%20'),
          };
          var request = https.request(options, function(response) {
            var chunks = [];
            response.on("data", function (chunk) {
              chunks.push(chunk);
            });
            response.on("end", function () {
              var body = Buffer.concat(chunks);
              console.log(body.toString());
              res.send(body);
              res.end()
            });
          });
          request.end();
        });

    Hope it helps.