Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
My express.js is not receiving the body that I sent from react native

I have a api object set up like this in react native:

import axios from "axios";
import AsyncStorage from "@react-native-async-storage/async-storage"; //npm install @react-native-async-storage/async-storage

const instance = axios.create({
  baseURL: "localhost url here",
});
/**
 * This will add a header if we have a token only,
 * we will be adding a Authorization header to our instance before running
 * the http req
 */
instance.interceptors.request.use(
  //this will be called before doing the http request,
  //it is async because to retrieve the storage it is async
  async (config) => {
    const token = await AsyncStorage.getItem("token"); //awaits until it gets token (IF THERE IS ONE)
    //if there is a token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`; //add string 'Bearer withGivenTOKEN'
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;

When doing the api call I am doing this:

await myApi
    .get("/routeHere", {
      latitude: currentLocation.latitude,
      longitude: currentLocation.longitude,
    })
    .then((response) => console.log(response))
    .catch((err) => console.log(err));

When receiving the data the body part is just a an empty object. Is there a reason why this happens? Am I doing something wrong?

router.get("/routeHere", async (req, res) => {
  console.log("here is my body: ", req.body);
}

I think I'm missing to add the header type, but not sure if it will work, and if it is, how can you write it? I'm new to express and react native

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

GET request shouldn't include data.

The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn't include data).

GET method

But you can use params to send the latitude and the longitude, like this:

await myApi
    .get(`/routeHere?latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}`)
    .then((response) => console.log(response))
    .catch((err) => console.log(err));

or

 await myApi
 .get("/routeHere", {
    params: {
      latitude: currentLocation.latitude,
      longitude: currentLocation.longitude,
    }
 })
.then((response) => console.log(response))
.catch((err) => console.log(err));

And you can receive it in the backend with req.query.latitude and req.query.longitude

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda