0

i have this route:

router.get("/filter",views.filter)

and this function:

function filter(req,res){
   console.log(req.body)]
   res.send('test')
}

that should receive data from a form via GET method, but always returns an empty object. When i change it to post, i can easily use body-parser to get the form data, but i need to do that with get method, how can i do this? I know that i could use the req object passed to the function to get the url of the form and use string methods to get a object with form Data, but i think that there is some express(or a library) function that does that.

Vitorfigmm
  • 23
  • 3
  • 3
    You should not use GET method to post data to the server. GET request should not have a body. What you can do is you can send data in a querystring in a url like so `/filter?hello=world` and access it like so `req.query.hello` – Molda Sep 23 '20 at 19:00
  • This [question](https://stackoverflow.com/questions/978061/http-get-with-request-body) might be of help – rantao Sep 23 '20 at 21:21
  • i'm using GET to give results to a search in a web page, POST wouldn't allow the result to be in the history of the browser. – Vitorfigmm Sep 23 '20 at 23:30
  • Molda, thanks, req.query[field] works fine for what i wanna do; – Vitorfigmm Sep 23 '20 at 23:42

0 Answers0