-2

I found that my HTTP delete request runs perfectly when I test it in Postman. However, when I try to test it in the browser, I receive a 'Cannot GET /account/delete/Julieth' error. I managed to resolve it by changing the method to GET, which works in both environments. Still, I'm curious why the browser doesn't recognize the DELETE method. Are there any constraints that the browser has against the DELETE method?

my code in index.js:

var express = require("express"); var app = express(); var cors = require("cors"); var bodyparser = require("body-parser"); app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json({ limit: "10mb" })); //serve static files app.use(express.static("public")); app.use(cors()); let users = [ { name: "Julieth", lastName: "Bautista", Email: "[email protected]", password: "bigsecret", balance: 0, }, { name: "Jose", lastName: "Bautista", Email: "[email protected]", password: "secret", balance: 233, }, ]; //create user route. GET should only retrieve data. app.get( "/account/create/:name/:lastname/:email/:password/:balance", function (req, res) { res.send({ name: req.params.name, lastName: req.params.lastname, email: req.params.email, password: req.params.password, balance: req.params.balance, }); } ); //GET should only retrieve data. app.get("/account/logIn/:email/:password", function (req, res) { res.send({ email: req.params.email, password: req.params.password, }); }); app.get("/account/all", function (req, res) { res.send(users); }); //post often causing a change in state or side effects on the server. app.post("/account", function (req, res) { var balance = req.body.balance; const user = req.body.name; const password = req.body.password; users.push({ name: user, password: password, balance: balance }); res.send(users); console.log("balance:" + balance); }); //PUT replaces all current representations of the target resource with the request payload. app.put("/account/withdraw", (req, res) => { console.log(users); const newArray = users.map((user) => { if (user.name == req.body.name && user.password == req.body.password) { if (user.balance == 0) return; else { newBalance = user.balance - req.body.balance; user.balance = newBalance; let myUser = user; let updatedUser = { balance: newBalance, ...myUser }; user = updatedUser; return user; } } return users; }); res.send(users); }); app.put("/account/deposit", (req, res) => { console.log(users); const newArray = users.map((user) => { if (user.name == req.body.name && user.password == req.body.password) { newBalance = req.body.balance + user.balance; user.balance = newBalance; let myUser = user; let updatedUser = { balance: newBalance, ...myUser }; user = updatedUser; return user; } return users; }); res.send(users); }); //deletes the specified resource. app.get("/account/delete/:name", function (req, res) { console.log(users); let filteredArray = users.filter((user) => user.name !== req.params.name); users = filteredArray; res.send(users); res.sendStatus(204); }); //deletes the specified resource. but doesn work in browser app.delete("/account/delete/:name", function (req, res) { console.log(users); let filteredArray = users.filter((user) => user.name !== req.params.name); users = filteredArray; res.sendStatus(204); }); app.listen(3000, function () { console.log("Runing on port 3000!"); }); 

I fixed it by changing the method to GET. Also, I make sure I have configured my server to allow requests for cross origin requests. However, do you have any ideas on how to solve the problem without having to change delete to GET so that the browser reads it correctly?

8
  • 2
    How are you sending a request to the server? Commented Jun 2, 2024 at 21:31
  • I am in the process of unifying my frontend (React) with the backend (Express), and in the index.js file of my backend, I am testing routes by writing HTTP requests on the server side through the URL path Commented Jun 2, 2024 at 21:54
  • 1
    That does not answer my question. How is your client / browser sending requests to the server and where do you specify it should send it as a DELETE? Commented Jun 2, 2024 at 21:59
  • It looks like the client-side is just sending a GET request instead of DELETE. If you're using an HTML form to submit the request, beware that HTML forms can't send a DELETE request without extra JS. Otherwise, make sure you are specifying the request method DELETE in the client-side. Commented Jun 3, 2024 at 0:13
  • It will help more if you snip the frontend code for sending the request Commented Jun 3, 2024 at 0:14

2 Answers 2

0

Seems you are trying in browser search uri which is not possible.Browser will hit only GET method.You have to use library to call the delete api.ex. fetch,axios,ajax .etc (assuming that you are using react or jquery).

or

You can try following code in developer tool(console)

 fetch('/account/delete/anyname', { method: 'DELETE' }) .then(response => response.json()) .then(data => { console.log(data); }) .catch(error => { console.error("Error From server:", error); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

According to the error, you are attempting to make a GET request. The browser will recognize DELETE requests only if you send them using JS.

Example:

fetch('/account/delete/Julieth', { method: 'DELETE' }) .then(response => response.json()) .then(data => { console.log(data); }) .catch(error => { console.error('Error:', error); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.