0

I have this curl request that I have like to convert to axios.

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1
Host: stg-id.pp2.com.sg

client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion- 
type%3Ajwt-bearer&client_assertion=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLmNvbSIsInN1YiI6ImNsaWVudElkX2p3a3NfdXJsIiwiYXVkIjoiaHR0cHM6XC9cL3N0Zy1pZC5zaW5ncGFzcy5nb3Yuc2ciLCJleHAiOjE2MTQ2NDgwMDcsImlhdCI6MTYxNDY0Nzg4N30.AAAAAAAAAAAAAAAAAAAAAHOnvK8BByCVs4RFcqzASg432GrI7Vzb2YELLHmZ-tqXAAAAAAAAAAAAAAAAAAAAALDHq0692LcmUkbPjg8565OfnyFwmSZSs9ghSZv_cJj5&client_id=clientId_jwks_url&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fexample.com&code=n0esc3NRze7LTCu7iYzS6a5acc3f0ogp4

Below is my axios code but I am getting this error: Request failed with status code 400

 clientId="abcd1234"


  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=ISO-8859-1',
    host: 'stg-id.prime.com.sg',
  }

  const response = await axios
    .post(
      'https://stg-id.pp2.com/token',
      {
        client_assertion_type:
          'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
        client_assertion: accessToken,
        client_id: clientId,
        grant_type: 'authorization_code',
        redirect_uri: 'https://primuslogin.firebaseapp.com/callback/',
        code: code,
      },
      { headers: headers }
    )
    .then(
      (response) => {
        console.log(response)
        res.send(response)
      },
      (error) => {
        console.log(error)
        res.send(error)
      }
    )

How could the axios code be written? Any help would be greatly appreciated.

Below is a response from the remote server, I think the format is not correct:

{"client_assertion_type":"urn:ietf:params:oauth:client-assertion-type:jwt-bearer","client_assertion":"eyJhbGciOiJIcCI6IkpXVCJ9.eyJpc3MiOiJXZXU1NFpNT3dlS2tCVWc4dmw4cm1vZ1lXdGpmM1ZDNyIsInN1YiI6IldldTU0Wk1Pd2VLa0JVZzh2bDhybW9nWVd0amYzVkM3IiwiYXVkIjoiaHR0cHM6Ly9zdGctaWQuc2luZ3Bhc3MuZ292LnNnIiwiaWF0IjoxNjE0NjczNjgyLCJleHAiOjE2MTQ3NjAwODJ9.XnhNNAOKm-sVa5rRWvFTyF9VTLZK6eFt6nyeERgZR6g","client_id":"fasdg8vl8fasdfrmogYWtjf3VC7","grant_type":"authorization_code","redirect_uri":"https://primuslogin.firebaseapp.com/callback/","code":"ItUbr8-rtJJlWUTn1DwAick7fH1p4MTZrZQIVwLt-mo"}

Daryl Wong
  • 2,023
  • 5
  • 28
  • 61
  • This would be a problem about the type of data you are passing onto the server. The server already response `400` which means that it has received your request. I notice that on curl you are using `x-www-form-urlencoded` but on the axios side, you are passing everything as `post` method which put everything into the body params. While it should be on the url (that is the point of url encoded). You can take a look at the guide here https://flaviocopes.com/axios-urlencoded/ – Vũ Thành Tâm Mar 02 '21 at 08:03
  • @VũThànhTâm, there is actually a response from server on the string, do you see that that is a need to use the qs package as mentioned in the link? – Daryl Wong Mar 02 '21 at 08:37
  • Nope you do not need to that, I'm sure that you can quickly code an alternative using vanilla js. – Vũ Thành Tâm Mar 02 '21 at 11:53

2 Answers2

0

This is the way to call post or any other request Maybe your doing something wrong in syntax try in this way hopefully it will work Sample Example

axios({
  method: 'post',
  url: '/login',
  data: {
    firstName: 'Finn',
    lastName: 'Williams'
  }
});
Wajid
  • 593
  • 3
  • 11
0

Like many people, you are mixing the await syntax and the Promise-style (.then).

You can't await something and also chain it a .then() handler.

Either use Promise-style axios.post(...).then(...);

OR the await-style

const response = await axios.post(...);
console.log(response);
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Calm down mate, I am typing... but in short, you can use await with then in promise style with no problem. – Vũ Thành Tâm Mar 02 '21 at 07:45
  • Sorry took a while to find the post about using await with then. Since then() are basically a declaration of another promise that needs to be done. You can always work on a promise chain and combine it with an `await` to wait for the chain to finish. You can take further look here: https://stackoverflow.com/questions/55019621/using-async-await-and-then-together https://developers.google.com/web/fundamentals/primers/async-functions – Vũ Thành Tâm Mar 02 '21 at 07:56
  • @JeremyThille, you were actually right, thanks for spotting my mistake. I just rectified it and now I got a response! – Daryl Wong Mar 04 '21 at 14:39