Skip to content
Advertisement

Translating Curl Request with Form Data to Axios

I’m trying to mimic the following request using axios:

curl -i -k --tlsv1.2 -H "Accept:application/json" -H "Content-Type:application/x-www-form-urlencoded" -d "client_id=YOUR_CLIENT_ID" -d "client_secret=YOUR_CLIENT_SECRET" -d "grant_type=refresh_token" -d "refresh_token=REFRESH_TOKEN_FROM_ACCESS_TOKEN_RESPONSE" -X POST https://api-sandbox.capitalone.com/oauth2/token

More information: https://developer.capitalone.com/documentation/o-auth

I came up with the following code:

axios({method: ‘post’, url: ‘https://api-sandbox.capitalone.com/oauth2/token’, params: {client_id: ‘…’, client_secret: ‘…’, grant_type: ‘refresh_token’, refresh_token: ‘…’}, headers: {‘Content-Type’: ‘aplication/x-www-form-urlencoded’, Accept: ‘application/json’ }}).then(res => console.log(res)).catch(ex => console.log(ex))

This keeps timing out and not giving me any response, which leads me to believe my request is malformed. Am I doing something wrong in trying to create this request with form data using axios?

Advertisement

Answer

Paste your curl command into https://curlconverter.com/node-axios/ and it will convert it to

const axios = require('axios');

const response = await axios.post(
    'https://api-sandbox.capitalone.com/oauth2/token',
    new URLSearchParams({
        'client_id': 'YOUR_CLIENT_ID',
        'client_secret': 'YOUR_CLIENT_SECRET',
        'grant_type': 'refresh_token',
        'refresh_token': 'REFRESH_TOKEN_FROM_ACCESS_TOKEN_RESPONSE'
    }),
    {
        headers: {
            'Accept': 'application/json'
        }
    }
);
Advertisement