How to use PUT/POST/DELETE methods in NodeJS directly in browser?
router.put('/sync/:syncId', (req, res) => {
res.send(JSON.stringify({ "status": 200, "error": false, "response": "HELLO WORLD" }));
});
This is how I access the endpoint in the browser and Postman:
http://localhost:4600/v1/fetchprojdetails/delete/1
When I use the above code on browser, then the router kind of acts weird and doesn't work. It neither shows res.send() statement nor any console.log() statements if there are any in the above function but when I try the same thing on Postman using the respective HTTP method(PUT/POST/DELETE) then it works as it should. The same thing works when I change the above code and replace router.delete() with router.get().
Can someone please explain to me why this behavior is seen on browser and POSTMAN. Is it so that the browser can't directly understand PUT/POST and DELETE methods?
CodePudding user response:
Postman is a tool designed to make custom HTTP requests so developers can test APIs. Browsers are tools designed to display webpages and run web applications embedded in them.
If you shove a URL into the browser's address bar then it will make a GET request. The address bar is a UI that affords getting a webpage and displaying it to the user.
If you want to make a POST request then you need to load an HTML document containing either a <form ... method="POST">
or JavaScript that makes an Ajax request.
If you want to use any other verb then you must use an Ajax request.