当使用 aws amplify on react 调用 api 网关时,我该如何获取状态码?
When using aws amplify on react to make an call to api gateway, how do i go about getting the statuscode as well?
我的 API 网关(使用无服务器)已设置为正确响应:
function buildResponse(statusCode, body) {
// console.log(body);
return {
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(body)
};
}
将后端部署到 aws 后,我让客户端使用 React.js 和 aws amplify,我在其中调用:
return API.post("api", "/api");
问题
我从 api 调用中正确获取了令牌,但没有获取状态代码。我如何构建 api 以便我也可以从 API 网关获取状态代码?
例如,从 "get" 下的 aws amplify 文档中查看此内容。如果在 myInit 中包含 "response",您将能够获得包括状态码在内的整个 axios 对象。
let apiName = 'MyApiName';
let path = '/path';
let myInit = { // OPTIONAL
headers: {}, // OPTIONAL
response: true, // OPTIONAL (return the entire Axios response object instead of only response.data)
queryStringParameters: { // OPTIONAL
name: 'param'
}
}
API.get(apiName, path, myInit).then(response => {
// Add your code here
}).catch(error => {
console.log(error.response)
});
我的 API 网关(使用无服务器)已设置为正确响应:
function buildResponse(statusCode, body) {
// console.log(body);
return {
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(body)
};
}
将后端部署到 aws 后,我让客户端使用 React.js 和 aws amplify,我在其中调用:
return API.post("api", "/api");
问题 我从 api 调用中正确获取了令牌,但没有获取状态代码。我如何构建 api 以便我也可以从 API 网关获取状态代码?
例如,从 "get" 下的 aws amplify 文档中查看此内容。如果在 myInit 中包含 "response",您将能够获得包括状态码在内的整个 axios 对象。
let apiName = 'MyApiName';
let path = '/path';
let myInit = { // OPTIONAL
headers: {}, // OPTIONAL
response: true, // OPTIONAL (return the entire Axios response object instead of only response.data)
queryStringParameters: { // OPTIONAL
name: 'param'
}
}
API.get(apiName, path, myInit).then(response => {
// Add your code here
}).catch(error => {
console.log(error.response)
});