Daryl Wong
Daryl Wong

Reputation: 2443

Curl to Axios request

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"}

Upvotes: 0

Views: 1000

Answers (2)

Jeremy Thille
Jeremy Thille

Reputation: 26360

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);

Upvotes: 0

Wajid
Wajid

Reputation: 593

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'
  }
});

Upvotes: 0

Related Questions