Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptp5.js httpDo() Function

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: 
 

  • path: It is a string that denotes the path of the URL or file to load.
  • method: It is a string that denotes the method for the HTTP request. It can have the values “GET”, “POST”, or “PUT”. The default value is “GET”. It is an optional parameter.
  • datatype: It is a string that specifies the type of data that will be received. It can have the values of “json”, “jsonp”, “xml”, or “text”. It will default to ‘text’ is no parameter is specified. It is an optional parameter.
  • data: It is an Object or a Boolean value that specifies the parameter data passed with the request.
  • callback: It is a function which is called when this function executes successfully. The first argument for this function is the data returned from the API. It is an optional parameter.
  • errorCallback: It is a function which is called if there is any error in executing the function. The first argument for this function is the error response. It is an optional parameter.
  • options: It is an object which specifies the request options, as in the Fetch API reference.

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: 
 

javascript




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: 
 

httpDo-GET-POST

Example 2:
 

javascript




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: 
 

httpDo-PUT

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments