0

I want to use body param instead of query string in nodejs. So far i have prepared this code.

Router.js

router.get("/getUserDetailsbyId/:id", getUserDetailsbyId);

Controller.js

getUserDetailsbyId: (req, res) => {
        console.log(req.body);
      const id = req.params.id;
      getUserDetails(id, (err, results) => {
        if (err) {
          console.log(err);
          return;
        }
        if (!results) {
          return res.json({
            success: 0,
            message: "Record not Found"
          });
        }
        results.password = undefined;
        return res.json({
          success: 1,
          data: results
        });
      });
    },

I am running this url in Postman.

http://localhost:3000/getUserDetailsbyId

This is the body in GET request. { "id":1 }

Output

<pre>Cannot GET /getUserDetailsbyId</pre>

No idea what i am doing wrong.I tried both method to check data is coming or not. console.log(req.body); const id = req.params.id;

Edit

I have made some changes in the router.js

router.get('/getUserDetailsbyId', (req, res) => {
    const { id } = req.body;
    getUserDetailsbyId(res.id);
    }),

Now i need to understand how to get this data in controller or sql query.

Roxx
  • 3,738
  • 20
  • 92
  • 155
  • 1
    That's a _path_ parameter. And a GET body has no defined semantics. – jonrsharpe Jul 05 '23 at 20:36
  • You've literally defined the route as requiring an `:id` path parameter. If you don't want that, why is it there? What sort of clients do you expect to use this API? Keep in mind that web browsers **cannot** send a GET request with a body – Phil Jul 05 '23 at 23:13
  • @Phil you are right. I have checked many questions and blogs everywhere i can see two method like param or querystring. Its not an issue if i need to change the route. but i want to understand is there any other method which can be used to pass the data for GET request. – Roxx Jul 06 '23 at 04:53
  • You're mostly limited to URL path or query string. Some clients can send GET request bodies but the behaviour isn't well defined. See [HTTP GET with request body](https://stackoverflow.com/q/978061/283366) – Phil Jul 06 '23 at 06:09

1 Answers1

1

Sending body/payload in a GET request may cause some existing implementations to reject the request

let express = require("express");
let app = express();

//enable middleware to accept json on server request
app.use(express.json());

router.get("/getUserDetailsbyId/:id", getUserDetailsbyId);

Here the server API need params /:id so, send request with body like :
(example on localhost) : http://localhost:3000/getUserDetailsbyId/1

Jerry
  • 1,005
  • 2
  • 13
  • Thanks for your answer but i don't want the url to be accessed by this way. Is there any method where i can put it in json body instead of http://localhost:3000/getUserDetailsbyId/1 this method. – Roxx Jul 05 '23 at 20:57
  • the server api was not matching in case of yours, so you can just send the get request with body at http://localhost:3000/getUserDetailsbyId (on localhost) and the backend rest api is like this : router.get("/getUserDetailsbyId", getUserDetailsbyId); – Jerry Jul 05 '23 at 23:02
  • Thanks Aniket. just one query how to get the data whatever we are sending in body to getUserDetailsbyId or any validation. +1 – Roxx Jul 06 '23 at 04:54