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();
let api_url =
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();
let postData = { id: 10, name:
"Mark"
, email:
"mark@gfg.com"
};
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);
}