I’m trying to mimic the following request using axios:
JavaScript
x
2
1
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
2
More information: https://developer.capitalone.com/documentation/o-auth
I came up with the following code:
JavaScript
1
2
1
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))
2
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
JavaScript
1
17
17
1
const axios = require('axios');
2
3
const response = await axios.post(
4
'https://api-sandbox.capitalone.com/oauth2/token',
5
new URLSearchParams({
6
'client_id': 'YOUR_CLIENT_ID',
7
'client_secret': 'YOUR_CLIENT_SECRET',
8
'grant_type': 'refresh_token',
9
'refresh_token': 'REFRESH_TOKEN_FROM_ACCESS_TOKEN_RESPONSE'
10
}),
11
{
12
headers: {
13
'Accept': 'application/json'
14
}
15
}
16
);
17