0

I am trying to modify the body of GET request. For that I tried this:

function modify(req, res, next){
  res.body = res.body + "modified";

  next();
}

then when I try to log res.body it prints nothing

Ref: Connect or Express middleware to modify the response.body

Ndjxh
  • 1
  • 4
  • https://stackoverflow.com/questions/9896628/connect-or-express-middleware-to-modify-the-response-body#comment125292331_9897767 – Ndjxh Nov 02 '22 at 10:58
  • GET request doesn't allow to pass body cause it is meant to get from server without modifying the existent data in db (say). Body in GET request doesn't make any sense at all. where as POST usually used for creating and PUT for updating data is widely used and meant for it for the same you pass body in it (not the only reason). – Nabhag Motivaras Nov 02 '22 at 18:41
  • Does this answer your question? [Connect or Express middleware to modify the response.body](https://stackoverflow.com/questions/9896628/connect-or-express-middleware-to-modify-the-response-body) – Evgeny Bovykin Nov 03 '22 at 13:01
  • @EvgenyBovykin I have already mentioned that in the question, that I tried that and its not working – Ndjxh Nov 04 '22 at 07:29
  • You are trying to modify response body, but then ask why request body is not changed. Request and response are 2 different things – Evgeny Bovykin Nov 04 '22 at 15:09
  • @EvgenyBovykin Sorry, that was changed during edit of question by someone else – Ndjxh Nov 05 '22 at 18:45

2 Answers2

0

There is no such thing as res.body. The body of the HTTP response can be created by many different methods, for example res.send or res.write in combination with res.end. For this reason, there is no standard way to "inject" some text into the body. In particular, it is impossible to add anything after sending of the body has been completed with res.end.

If you could specify how the exisiting middleware creates the response body, we could judge whether it is possible to modify this behavior through addition of another middleware. But the additional middleware will depend heavily on the existing middleware, and it might be easier to achieve your goal by modifying the existing middleware.

Heiko Theißen
  • 12,807
  • 2
  • 7
  • 31
  • If I fetch bundle.js from `http://localhost:8080/bundle.js` but I want to merge two files bundle.js and abc.js. Can I achieve that with res.send or res.write in combination with res.end. – Ndjxh Nov 06 '22 at 05:24
  • Found it: https://stackoverflow.com/questions/41410660/node-js-express-how-do-i-send-response-as-javascript-or-other-file-type – Ndjxh Nov 06 '22 at 07:20
-1

To retrieve values from GET method you need to used 'req.params' followed by the name of parameter.

for exemple 'req.params.id'

Drashti Kheni
  • 1,065
  • 9
  • 23
  • But how will it give `response` body – Ndjxh Nov 02 '22 at 11:27
  • req.body is for POST request,, req.params for GET request. Your values come from what type of method? GET or POST? show more code pls. – Terry caloc Nov 02 '22 at 11:32
  • For GET request I am getting req.params as empty object – Ndjxh Nov 02 '22 at 11:37
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 05 '22 at 21:07