使用 axios 发送 application/x-www-form-urlencoded 的正确方法是什么?
我正在遵循此处提供的文档: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;
}
}
但是,这会返回 400。下面给出了完整的错误以及发送的请求。我已用虚拟文本替换了秘密,但它们是正确的。
{
"message": "Request failed with status code 400",
"name": "AxiosError",
"stack": "AxiosError: Request failed with status code 400\n 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
}
如果我使用包请求,代码可以正常工作。
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);
});
});
}
有人可以告诉我 Axios 做错了什么吗?
尝试将
params
对象包含在 config
中:
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",
},
}
);