【发布时间】:2021-03-31 00:59:38
【问题描述】:
我用 Postman 测试了一个 API,想用 axios 写一个请求。这就是它在 Postman 中的样子,并且运行良好。
这是我的 javascript 代码:
axios.post('http://127.0.0.1:8080/auth/realms/master/protocol/openid-connect/token', null,
{ params: {
grant_type : "password",
username: "test",
password: "password",
client_id: "admin-cli"
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
}).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
})
这会导致以下错误:
POST http://127.0.0.1:8080/auth/realms/master/protocol/openid-connect/token?grant_type=password&username=test&password=password&client_id=admin-cli 400 (Bad Request)
我也尝试使用 curl,它也可以:
curl -X POST http://127.0.0.1:8080/auth/realms/master/protocol/openid-connect/token -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=password&username=test&password=password&client_id=admin-cli'
我猜我的 axios 代码可能有错误,但我不知道可能出了什么问题 :-(
【问题讨论】: