Open In App

p5.js httpDo() Function

The httpDo() function in p5.js is used to execute an HTTP request. The type of HTTP request can be specified as a parameter, which is by default an HTTP request. The datatype returned is automatically guessed by p5 based on the URL, when it is not specified. The options parameter can be used to specify advanced properties according to the Fetch API specifications.
Syntax: 
 

httpDo( path, [method], [datatype], [data], [callback], [errorCallback] )

OR 
 



httpDo( path, options, [callback], [errorCallback] )

Parameters: This function accept seven parameters as mentioned above and described below: 
 

Return Value: It returns a promise that can be resolved with the data when the operation completes successfully, or be rejected when an error takes place.
Below example illustrates the httpDo() function in p5.js:
Example 1: 
 






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: "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);
}

Output: 
 

Example 2:
 




function setup() {
  createCanvas(550, 300);
  textSize(18);
  
  text("Click on the button below to send a PUT request.", 20, 40);
  
  getBtn = createButton("PUT Request");
  getBtn.position(30, 60);
  getBtn.mouseClicked(putRequest);
}
  
function putRequest() {
  
  let putData = { name: "Dan", email: "dan@gfg.com" };
  
  // Get a random user from the test api
  let api_url =
    'https://reqres.in/api/users/' + int(random(1, 10));
  
  httpDo(api_url, "PUT", "json", putData, function (response) {
    text("Data returned from API", 20, 140);
      
    text("The updated name is: " + response.name, 20, 180);
    text("The updated email is: " + response.email, 20, 200);
    text("Data is updated at: " + response.updatedAt, 20, 220);
  });
}

Output: 
 

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/amp/
Reference: https://p5js.org/reference/#/p5/httpDo
 


Article Tags :