Skip to content

Instantly share code, notes, and snippets.

@amottier
Created March 21, 2019 18:40
Show Gist options
  • Save amottier/aa0758d9a7e5a8a99d1afbbd655a7e0f to your computer and use it in GitHub Desktop.
Save amottier/aa0758d9a7e5a8a99d1afbbd655a7e0f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test API</title>
<script type="text/javascript">
// HTTP call to the login service
var loginRequest = new XMLHttpRequest();
// Asynchronous HTTP POST call to login API
loginRequest.open("POST", "http://compute.amazonaws.com:8080/bonita/loginservice", true);
// Content-Type for the login operation
loginRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// To store cookie
loginRequest.withCredentials = true;
// Handle asynchronous call response
loginRequest.onreadystatechange = function() {
if(loginRequest.readyState === XMLHttpRequest.DONE) {
console.log("Login request response HTTP status: " + loginRequest.status);
// HTTP call to get user session information
var sessionInfoRequest = new XMLHttpRequest();
sessionInfoRequest.open("GET", "http://compute.amazonaws.com:8080/bonita/API/system/session/1", true);
sessionInfoRequest.withCredentials = true;
// Handle asynchronous call to session API response
sessionInfoRequest.onreadystatechange = function() {
if(sessionInfoRequest.readyState === XMLHttpRequest.DONE) {
console.log("Get user session information response HTTP status: " + sessionInfoRequest.status);
var xBonitaAPIToken = sessionInfoRequest.getResponseHeader("X-Bonita-API-Token");
console.log("X-Bonita-API-Token from the HTTP header of session API response: " + xBonitaAPIToken);
// Update the information of a user
var updateUserRequest = new XMLHttpRequest();
updateUserRequest.open("PUT", "http://compute.amazonaws.com:8080/bonita/API/identity/user/1", true);
updateUserRequest.withCredentials = true;
// Content of the PUT request is JSON
updateUserRequest.setRequestHeader("Content-Type", "application/json");
// Add the X-Bonita-API-Token header with the value we get from the session API call response
updateUserRequest.setRequestHeader("X-Bonita-API-Token", xBonitaAPIToken);
// Data to use for the update
var formData = {"title":"Mr","manager_id":"0","job_title":"Chief Executive Officer","lastname":"Jobs","firstname":"Wil"};
updateUserRequest.onreadystatechange = function() {
if(updateUserRequest.readyState === XMLHttpRequest.DONE) {
console.log("Update user information response HTTP status: " + updateUserRequest.status);
}
}
updateUserRequest.send(JSON.stringify(formData));
}
}
sessionInfoRequest.send();
}
}
loginRequest.send("username=walter.bates&password=bpm&redirect=false");
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment