function setup() {
createCanvas(550, 300);
textSize(18);
text("Click on the button below to send a GET "
+ "or POST request.", 20, 40);
getBtn = createButton("GET Request");
getBtn.position(30, 60);
getBtn.mouseClicked(getRequest);
postBtn = createButton("POST Request");
postBtn.position(30, 90);
postBtn.mouseClicked(postRequest);
}
function getRequest() {
clear();
// Get a random user from the test api
let api_url =
'https://reqres.in/api/users/' + int(random(1, 10));
httpDo(api_url, "GET", "json", false, function (response) {
text("Data fetched from API", 20, 140);
text("The First Name in the data is: "
+ response.data.first_name, 20, 180);
text("The Last Name in the data is: "
+ response.data.last_name, 20, 200);
text("The Email in the data is: "
+ response.data.email, 20, 220);
});
text("Click on the button below to send a "
+ "GET or POST request.", 20, 40);
}
function postRequest() {
clear();
// Do a POST request to the test API
let api_url = 'https://reqres.in/api/users';
// Example POST data
let postData = { id: 10, name: "Mark", email: "[email protected]" };
httpDo(api_url, "POST", "json", postData, function (response) {
text("Data returned from API", 20, 140);
text("The ID in the data is: " + response.id, 20, 180);
text("The Name in the data is: " + response.name, 20, 200);
text("The Email in the data is: " + response.email, 20, 220);
});
text("Click on the button below to send a "
+ "GET or POST request.", 20, 40);
}