What is the correct way to send application/x-www-form-urlencoded with axios?
I am following the documentation provided here: https://github.com/axios/axios?tab=readme-ov-file#using-applicationx-www-form-urlencoded-format
async function loginAuth(code: string) {
try {
const params = new URLSearchParams({
grant_type: "authorization_code",
code,
client_id: process.env.COGNITO_CLIENT_ID,
redirect_uri: process.env.COGNITO_REDIRECT_URL,
});
const response = await axios.post(
`https://${process.env.COGNITO_CLIENT_NAME}.auth.us-east-1.amazoncognito.com/oauth2/token`,
params,
{
headers: {
Authorization: `Basic ${process.env.COGNITO_AUTHENTICATION}`,
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
return response.data;
} catch (e) {
console.error(`Error with loginAuth: ${JSON.stringify(e)}`);
return null;
}
}
However, this returns in a 400. The complete error, along with the request that is sent is given below. I have replaced the secrets with dummy text, but they are correct.
{
"message": "Request failed with status code 400",
"name": "AxiosError",
"stack": "AxiosError: Request failed with status code 400n at settle (/Users/rahul/code/helix-transition_graphql/packages/api/node_modules/axios/lib/core/settle.js:19:12)n at IncomingMessage.handleStreamEnd (/Users/rahul/code/helix-transition_graphql/packages/api/node_modules/axios/lib/adapters/http.js:589:11)n at IncomingMessage.emit (node:events:531:35)n at IncomingMessage.emit (node:domain:488:12)n at endReadableNT (node:internal/streams/readable:1696:12)n at processTicksAndRejections (node:internal/process/task_queues:82:21)n at Axios.request (/Users/rahul/code/helix-transition_graphql/packages/api/node_modules/axios/lib/core/Axios.js:45:41)n at processTicksAndRejections (node:internal/process/task_queues:95:5)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"adapter": [
"xhr",
"http",
"fetch"
],
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic <dummy_token>",
"User-Agent": "axios/1.7.2",
"Content-Length": "158",
"Accept-Encoding": "gzip, compress, deflate, br"
},
"method": "post",
"url": "dummy_url",
"data": "grant_type=authorization_code&code=dummy_code&client_id=dummy_idredirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth"
},
"code": "ERR_BAD_REQUEST",
"status": 400
}
If I use the package request, the code works without any issue.
function loginAuth(code: string) {
return new Promise((resolve, reject) => {
const options = {
method: "POST",
url: `https://${process.env.COGNITO_CLIENT_NAME}.auth.us-east-1.amazoncognito.com/oauth2/token`,
headers: {
Authorization: `Basic ${process.env.COGNITO_AUTHENTICATION}`,
"Content-Type": "application/x-www-form-urlencoded",
},
form: {
grant_type: "authorization_code",
code,
client_id: process.env.COGNITO_CLIENT_ID,
redirect_uri: process.env.COGNITO_REDIRECT_URL,
},
};
request(options, (error, response) => {
if (error) {
reject(error);
return;
}
resolve(response.body);
});
});
}
Can someone tell me what I am doing wrong with Axios?
1