我正在尝试创建一个小型应用,并且我正在通过返回Cookie的api登录。我在应用程序中查找和处理Cookie时遇到问题。有什么建议吗?
我的提取方法:
async login() {
try {
let response = await fetch('http://192.168.168.114:8090/Cam/LoginOut/role', {
credentials: 'include',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
role: "CamTherapist",
username: this.state.username,
password: this.state.password
})
});
let res = await response.text();
if(response.status >= 200 && response.status < 300) {
let accessToken = res;
console.log("res is + " + res);
this.props.navigation.navigate('Idag');
} else {
this.props.navigation.navigate('Idag');
let errors = res;
console.log(response.status + " fejl, Jeg er næsten inde");
throw errors;
}
} catch (errors) {
console.log('There has been a problem with your fetch operation: ');
// ADD THROW ERRORS EVENTUALLY
}
}
答案 0 :(得分:0)
Cookie通常作为称为Set-Cookie
的标头发送。您可以使用response.headers
找到响应标题。
因此,为了对您的cookie进行本地化:
let headers = response.headers
let newCookie = headers['Set-Cookie']
请注意,Set-Cookie
标头包含cookie键,其值以及一些其他信息,例如到期日期。您将需要解析字符串以检测特定信息。
请务必注意,在React Native中,fetch应该支持现成的cookie,因此您应该已经可以在后续请求中使用cookie,而无需任何其他代码