How To Use Req.body Via Get Request In Nodejs
I have a form which uses a GET method. i also have an input with the name 'a'. when i handle the request on the server side (nodejs) i want to be able to use req.body.a (in order
Solution 1:
If you are using GET
method then the data is sent as query parameters
req.query
By the way there will be no body for GET
method. If you want to send data through body use POST
or PUT
method.
Solution 2:
You can access req.body
in GET
method just as you would in a POST
method. Here's an example:
exportconstgetFile = (req, res) => {
const { fileId } = req.body;
console.log(fileId)
}
Although you CAN do this, I would suggest avoiding it because it goes against HTTP conventions. Instead, put whatever data you want in the URL parameters, which you can access in Node.JS using req.params
.
Post a Comment for "How To Use Req.body Via Get Request In Nodejs"