0

Hi guys just what the title says, I couldn't find a solution.

Here the cURl:

curl "https://test.api.amadeus.com/v1/security/oauth2/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}"

Here is my code that is getting a 400 error from server:

const getToken = async () => {
try {
  const res = await axios.post(
    "https://test.api.amadeus.com/v1/security/oauth2/token",
    null,
    {
      params: {
        client_id: process.env.REACT_APP_AMADEUS_API_KEY,
        client_secret: process.env.REACT_APP_AMADEUS_SECRET_API_KEY,
        grant_type: "client_credentials",
      },
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    }
  );
  console.log(res);
} catch (error) {
  console.log(error);
}};
  • Maybe this will help: https://stackoverflow.com/questions/55557557/axios-post-results-in-bad-request-grant-typeclient-credentials – first last Feb 01 '22 at 03:23

2 Answers2

0

curl -d sends data as application/x-www-form-urlencoded body. params: config in axios.post sends data in query string. Use data: instead, or just specify the data in the second argument (replacing your null).

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

axios post method should have second parameter as body params and third parameter as config object like headers. below code might help you.

const getToken = async () => {
try {
  const res = await axios.post(
    "https://test.api.amadeus.com/v1/security/oauth2/token",
    {
        client_id: process.env.REACT_APP_AMADEUS_API_KEY,
        client_secret: process.env.REACT_APP_AMADEUS_SECRET_API_KEY,
        grant_type: "client_credentials",
      },
    {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    }
  );
  console.log(res);
} catch (error) {
  console.log(error);
}};
Dhananjayan
  • 562
  • 5
  • 23