[][src]Static p5_sys::global::httpDo

pub static httpDo: HttpDoInternalType

Method for executing an HTTP request. If data type is not specified, p5 will try to guess based on the URL, defaulting to text.

For more advanced use, you may also pass in the path as the first argument and a object as the second argument, the signature follows the one specified in the Fetch API specification. This method is suitable for fetching files up to size of 64MB when "GET" is used.

Examples

// Examples use USGS Earthquake API:
// https://earthquake.usgs.gov/fdsnws/event/1/#methods

// displays an animation of all USGS earthquakes
let earthquakes;
let eqFeatureIndex = 0;

function preload() {
  let url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson';
  httpDo(
    url,
    {
      method: 'GET',
      // Other Request options, like special headers for apis
      headers: { authorization: 'Bearer secretKey' }
    },
    function(res) {
      earthquakes = res;
    }
  );
}

function draw() {
  // wait until the data is loaded
  if (!earthquakes || !earthquakes.features[eqFeatureIndex]) {
    return;
  }
  clear();

  let feature = earthquakes.features[eqFeatureIndex];
  let mag = feature.properties.mag;
  let rad = mag / 11 * ((width + height) / 2);
  fill(255, 0, 0, 100);
  ellipse(width / 2 + random(-2, 2), height / 2 + random(-2, 2), rad, rad);

  if (eqFeatureIndex >= earthquakes.features.length) {
    eqFeatureIndex = 0;
  } else {
    eqFeatureIndex += 1;
  }
}

Overloads

path name of the file or url to load

method? either "GET", "POST", or "PUT", defaults to "GET"

datatype? "json", "jsonp", "xml", or "text"

data? param data passed sent with request

callback? function to be executed after httpGet() completes, data is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument


path name of the file or url to load

options Request object options as documented in the "fetch" API reference

callback? function to be executed after httpGet() completes, data is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument