Home > Enterprise >  Why is req.body empty {} (GET REQUEST)
Why is req.body empty {} (GET REQUEST)

Time:12-01

This is my Frontend code

  const fetchData = () => {
    const options = {
        method: 'GET',
        url: 'http://localhost:1337/user/chart',
        headers: {'x-access-token': sessionStorage.getItem('token')},
        body: [chartData.datasets]
      }
      axios.request(options).then((response) => {
        console.log(response)
        }).catch((error) => {
        console.error(error)})
}

This is backend

app.get('/user/chart', async (req, res) => {
    const token = req.headers['x-access-token']
    if (!token){
        return res.status(404).json({ success: false, msg: "Token not found" });
       }

    try {
        const decoded = jwt.verify(token, process.env.access_secret)
        const email = decoded.email
        await User.updateOne(
            { email: email },
            { $set: {} },

        )
        console.log(req.body)
        return res.status(200).json({message: 'ok', label:[]})
    } catch (error) {
        console.log(error)
        res.json({ status: 'error', error: 'invalid token' })
    }

})

When I console.log(req.body) it is an empty {}. Why is it empty? I am using a GET request to retrieve the chart data

CodePudding user response:

Axios API does not accept body on get get request you can send parameters with params example

const url = '/user/chart';

const config = {
  headers: {'x-access-token': sessionStorage.getItem('token')},
  params:{someKey:chartData.datasets}
};

axios.get(url, config)

CodePudding user response:

Axios doesn't support setting a body for a get request, see the docs or this related question.

Though, I'd also recommend to reconsider your design. Typically the body isn't used in a GET request. If you're sending data to the server, you likely want to use POST or PUT instead. If you just want to pass a parameter, then you likely want to use request parameters.

If you absolutely need to send a body in your GET request, then you'll need to use a different tool.

CodePudding user response:

frondend //

const fetchData = () => {
const options = {
    method: 'POST',
    url: 'http://localhost:1337/user/chart',
    headers: {'x-access-token': sessionStorage.getItem('token')},
    body: {name : "xx",mail:"xx@"}
  }
  axios.request(options).then((response) => {
    console.log(response)
    }).catch((error) => {
    console.error(error)})
 }

backend //

app.post('/user/chart', async (req, res) => {
const {name , mail} = req.body
const token = req.headers['x-access-token']
if (!token){
    return res.status(404).json({ success: false, msg: "Token not found" });
   }

try {
    const decoded = jwt.verify(token, process.env.access_secret)
    const email = decoded.email
    await User.updateOne(
        { email: email },
        { $set: {} },

    )
    console.log(req.body)
    return res.status(200).json({message: 'ok', label:[]})
} catch (error) {
    console.log(error)
    res.json({ status: 'error', error: 'invalid token' })
}

})Ï

  • Related