axios.post 返回 400 React Native 的错误请求 [英] axios.post returns bad request of 400 React Native
问题描述
我从 API 获取令牌,但不幸的是我的 API 返回 400 错误请求.我已经通过 Postman 检查了我的 api,它在那里工作正常.请让我知道解决方案或任何错误.
I'm getting my token from an API but unfortunately my API is returning 400 bad request. I've already checked my api via Postman and it's working fine there. Kindly let me know solution or any mistake.
async componentWillMount(){
axios.post('http://api.myapiurl.com/token', {
grant_type: 'PASSWORD',
username: 'MY_USERNAME',
password: 'MY_PASSWORD'
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then(response => {
console.log(response.data)
}).catch(err => console.log("api Erorr: ", err.message))
}
以下回复有误
请求失败,状态码 400- node_modulesaxioslibcorecreateError.js:16:24 in createError- node_modulesaxioslibcoresettle.js:18:6 在解决- ... 来自框架内部的另外 10 个堆栈帧
Request failed with status code 400 - node_modulesaxioslibcorecreateError.js:16:24 in createError - node_modulesaxioslibcoresettle.js:18:6 in settle - ... 10 more stack frames from framework internals
推荐答案
使用QueryString.stringify()
解决.我只是将正文传递给 QueryString.stringify()
,如下所示:
It is Solved by using QueryString.stringify()
. I just pass the body into QueryString.stringify()
like below:
axios.post('http://api.apiurl.com/token', QueryString.stringify({
grant_type: 'MY_GRANT_TYPE',
username: 'MY_USERNAME',
password: 'MY_PASSWORD'
}), {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
}
}).then(response => {
console.log(response.data)
}).catch(err => console.log("api Erorr: ", err.response))
这篇关于axios.post 返回 400 React Native 的错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!