2

I am trying to make a get request with JSON payload, but the data never gets to the server, req.body is empty.

  async function getConfig() {
    try {
      const retrievedConfig = await axios.get(
        "http://localhost:3000/config/one",
        {
          Name: "Hello",
        }
      );
      console.log(retrievedConfig);
    } catch (e) {
      console.log("Error");
      console.log(e);
    }
  }

I have tried stringifying it, I have also tried adding headers as a third argument { headers } taken from this question here Post request with axios and json data

  const headers = {
    Accept: "application/json",
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
    "Access-Control-Allow-Headers": "Authorization",
  };

But no matter what I do, req.body on the server side is empty.

If I do (in the same function):

const retrievedConfig = await axios.get("http://localhost:3000/config/all");
console.log(retrievedConfig);

To get all configs, it works and I do get the configs, so the problem should be with how I am sending the JSON object.

The same request with Postman works as intended, with the same json payload. http://prntscr.com/twizp9

P.S. This is axios on the front end (react) trying to get data from the backend (node)

Darkbound
  • 3,026
  • 7
  • 34
  • 72
  • the `data` parameter is "Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'" https://github.com/axios/axios#request-config – Aprillion Aug 09 '20 at 06:51
  • 1
    Does this answer your question? [Axios get in url works but with second parameter as object it doesn't](https://stackoverflow.com/questions/40947650/axios-get-in-url-works-but-with-second-parameter-as-object-it-doesnt) – Prathap Reddy Aug 09 '20 at 06:52
  • 1
    `Axios` work little different (options are little different). That might the reason why it's working in `postaman` and not with `axios` request. – Prathap Reddy Aug 09 '20 at 06:53
  • 1
    @PrathapReddy yes that worked, thanks. – Darkbound Aug 09 '20 at 06:58

0 Answers0