Adil Ali
Adil Ali

Reputation: 29

Why am I getting a 404 status error on my PUT request?

I'm getting a HTTP 404 on a PUT request as shown below:

enter image description here

$(function(){
$("#showMovies").click(function() {
  $.ajax({
    method:"GET",
    url: "http://localhost:3000/movielist",
    dataType: "json",
    success: function (response) {
      $.each(response, function(i, movie) {
        const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
  });
});

$("#movieAdded").click(function() {
  $.ajax({
    method:"POST",
    url: "http://localhost:3000/movielist/addMovie",
    dataType: "json",
    data: {
       idmovielist: 10,
       name: 'Bubble Gum',
       thumnail_path: 'yourieiri.jpg',
       description: 'Disturbing',
       year_released: '2007',
       language_released: 'french'
    },
    success: function (data) {
      $.each(data, function(i, movie) {
        const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
  });
});
$.ajax({
   method:"DELETE",
   url: "http://localhost:3000/movielist/8",
   dataType: "json",
   success: function (data) {
     $.each(data, function(i, movie) {
    const rowText = "<tr>" +
         "<td>" + movie.idmovielist + "</td>" +
         "<td>" + movie.name + "</td>" +
         "<td>" + movie.thumbnail_path + "</td>" +
         "<td>" + movie.description + "</td>" +
         "<td>" + movie.year_released + "</td>" +
         "<td>" + movie.language_released + "</td>" +
         "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
         "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
       $("#movies").append(rowText);
     });
   }
});
$.ajax({
    method:"PUT",
    url: "http://localhost:3000/movielist/6",
    dataType: "json",
    data: {
        idmovielist: 6,
        name: 'Lion King',
        thumanail_path: 'https://lumiere-a.akamaihd.net/v1/images/',
        description: 'cartoon',
        year_realeased: '2000',
        language_released: 'english'
    },
    success: function (data) {
      $.each(data, function(i, movie) {
       const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
});
});

So whenever I try to execute my put request I always get this error of status code 404 not found and also My get post and delete are working just fine it is just my put request what can I do to fix it. Also I was able to get this working n my web browser because I forgot to add a function

app.put('/movielist/id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results)  => {
    if (!err) {
      res.send("Movie list is updated");
    } else {
      console.log(err);
    }
});
});

Also my post request code

app.post('/movielist/addMovie',(req, res) => {
   mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
   req.body,
   (err,rows) => {
     if (!err) {
       res.send("Movie is added");
     } else {
       console.log(err);
     }
  });
});


  app.get('/movielist',(req,res)=> {
    mysqlConnection.query("SELECT * FROM movielist", (err, rows,fields)=> {
      if (!err) {
        res.send(rows);
      } else {
        console.log(err);
      }
    });
  });
 app.delete('/movielist/:id',(req,res) => {
    mysqlConnection.query("DELETE FROM movielist WHERE idmovielist = ?",[req.params.id],(err,rows,fields) =>{
      if (!err) {
        res.send("Movie is deleted");
      } else {
      console.log(err);
    }
    });
  });
app.post('/movielist/addMovie',(req, res) => {
   mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
   req.body,
   (err,rows) => {
     if (!err) {
       res.send("Movie is added");
     } else {
       console.log(err);
     }
  });
});
app.put('/movielist/:id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results)  => {
    if (!err) {
      res.send("Movie list is updated");
    } else {
      console.log(err);
    }
});
});

Postman

Upvotes: 1

Views: 9318

Answers (1)

I think you need to change this:

app.put('/movielist/id',(req,res) =>{

to this:

app.put('/movielist/:id',(req,res) =>{

In nodejs with express framework, in order to use /link/parameter/, parameter needs to be declared with :.

Upvotes: 1

Related Questions