0

Axios not sending body to my API.

router.get('/login', async function(req,res) {
  try {
    const email  = req.body.email;
    const pass  = req.body.pass;
    const users = await pool.query("select username from users where username=$1 and password=$2", [email, pass]);
    if(users.rows[0])
      res.json(users.rows[0]['username']!=null);
    else
      res.json(users)
      console.log(req.body)
    console.log(email,pass)
  } catch (err) {
    console.log(err.message);
  }
});

When I console.log the req.body it after running GetData

async function GetData() {

    const email = document.getElementById("email").value;
    const pass = document.getElementById("pass").value;
    const response = await axios.get("http://localhost:3002/login",
    {
      body: JSON.stringify({"email":  email, "pass" : pass})
    })
    console.log(response.config.body);
    console.log(response.data.rows)
  }

The body is said to be {} an empty object.

How do I send the body through axios the same way insomnia is sending a body.

When using insomnia and the exact same body it returns true as it should. enter image description here

  • try using `post` instead of `get` – Derple Sep 22 '21 at 03:16
  • 1
    Does this answer your question? [body data not sent in axios request](https://stackoverflow.com/questions/52561124/body-data-not-sent-in-axios-request) – Derple Sep 22 '21 at 03:17

1 Answers1

0

GET doesn’t accept sending a body, use POST instead:

router.post('/login', async function(req,res) {

// …

const response = await axios.post("http://localhost:3002/login", { email, pass })

Also, the response.config.body is not a thing, if you look at request config documentation, there is the property “data”, rather than “body”.

console.log(response.config.data);

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91