React native get API endpoint always 400 without body
React native get API endpoint always 400 without body
我有一个正常工作的 CURL 请求:
curl -v -X POST https://auth.domain.com/v1/oauth/tokens -u test:test -d "grant_type=authorization_code" -d "code=d9a473a4-e417-4dd7-9151-83e9c1cb9ca6" -d "redirect_uri=app://authorize"
我尝试在我的 React Native 应用程序中实现它,但我总是收到 400 错误。首先我使用了 axios:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url, {
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
},{
auth: {
username: 'test',
password: 'test'
}
}).then(response => {
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
throw error
});
但是我得到了 400 错误:
Possible Unhandled Promise Rejection (id: 0):
Request failed with status code 400
Error: Request failed with status code 400
我用 fetch 试过:
fetch(url, {
method: 'post',
headers: {
'Authorization': 'Basic '+btoa('test:test'),
},
body: JSON.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
})
}).then(response => {
console.log('Request core...');
console.log(response);
})
同样的空主体出现 400 错误。对于 CURL 请求,我得到了 200 OK 和来自服务器的响应。我在 JS 方面做错了什么?
正如Yury Tarabanko提到的那样,问题是"You are sending urlencoded data using curl. Other examples send json. "
解决方案:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url,
querystring.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
}),{
auth: {
username: 'test',
password: 'test'
},
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log('Request core...');
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
console.log(error);
throw error;
});
这段代码适合我。
.catch((error) => console.log( error.response.request._response ) );
我有一个正常工作的 CURL 请求:
curl -v -X POST https://auth.domain.com/v1/oauth/tokens -u test:test -d "grant_type=authorization_code" -d "code=d9a473a4-e417-4dd7-9151-83e9c1cb9ca6" -d "redirect_uri=app://authorize"
我尝试在我的 React Native 应用程序中实现它,但我总是收到 400 错误。首先我使用了 axios:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url, {
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
},{
auth: {
username: 'test',
password: 'test'
}
}).then(response => {
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
throw error
});
但是我得到了 400 错误:
Possible Unhandled Promise Rejection (id: 0):
Request failed with status code 400
Error: Request failed with status code 400
我用 fetch 试过:
fetch(url, {
method: 'post',
headers: {
'Authorization': 'Basic '+btoa('test:test'),
},
body: JSON.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
})
}).then(response => {
console.log('Request core...');
console.log(response);
})
同样的空主体出现 400 错误。对于 CURL 请求,我得到了 200 OK 和来自服务器的响应。我在 JS 方面做错了什么?
正如Yury Tarabanko提到的那样,问题是"You are sending urlencoded data using curl. Other examples send json. "
解决方案:
var url = `https://auth.domain.com/v1/oauth/tokens`
axios.post(url,
querystring.stringify({
"grant_type": 'authorization_code',
"code": code,
"redirect_uri": 'app://authorize',
}),{
auth: {
username: 'test',
password: 'test'
},
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log('Request core...');
console.log(response);
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
console.log(error);
throw error;
});
这段代码适合我。
.catch((error) => console.log( error.response.request._response ) );