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.
httpDo( path, [method], [datatype], [data], [callback], [errorCallback] )
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:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/