I am using the CheckPoint API in my web app and I am trying to store the "sid" in a variable. How can I do this? Below is the code snippet that I use to login
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({ user: "Licenta2020", password: "Licenta2020" });
var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
fetch("https://192.168.100.100/web_api/v1.6/login", requestOptions)
.then((response) => response.text())
.then((result) => {
console.log(result);
})
.catch((error) => console.log("error", error));
And here is the result of this request, I want to take the sid value and assign to a variable so I can use it later on for other requests.
{
"uid" : "4a587f4b-6a0a-4e0f-a1f4-cb99b19657dd",
"sid" : "ite95YKJ7gmV-7s4YJbHIeAke9ODoklA5Z1yVHriRkA",
"url" : "https://192.168.100.100:443/web_api/v1.6",
"session-timeout" : 600,
"last-login-was-at" : {
"posix" : 1591398092564,
"iso-8601" : "2020-06-06T02:01+0300"
},
"disk-space-message" : "Partition /var/log has: 1354 MB of free space and it's lower than required: 2000 MB\n",
"api-server-version" : "1.6"
}
Tried this code snippet too
.then((result) => {
let sid = result.sid;
console.log(result);
console.log(sid);
})
you are returning text in promise as .text()
instead use .json();
that's because .json()
automatically parses to json for you. so after that you could continue to use result["sid"]
to get desired output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.