Static p5_sys::global::httpPost[][src]

pub static httpPost: HttpPostInternalType
Expand description

Method for executing an HTTP POST request. If data type is not specified, p5 will try to guess based on the URL, defaulting to text. This is equivalent to calling httpDo(path, 'POST').

Examples

// Examples use jsonplaceholder.typicode.com for a Mock Data API

let url = 'https://jsonplaceholder.typicode.com/posts';
let postData = { userId: 1, title: 'p5 Clicked!', body: 'p5.js is way cool.' };

function setup() {
  createCanvas(800, 800);
}

function mousePressed() {
  // Pick new random color values
  let r = random(255);
  let g = random(255);
  let b = random(255);

  httpPost(url, 'json', postData, function(result) {
    strokeWeight(2);
    stroke(r, g, b);
    fill(r, g, b, 127);
    ellipse(mouseX, mouseY, 200, 200);
    text(result.body, mouseX, mouseY);
  });
}
let url = 'ttps://invalidURL'; // A bad URL that will cause errors
let postData = { title: 'p5 Clicked!', body: 'p5.js is way cool.' };

function setup() {
  createCanvas(800, 800);
}

function mousePressed() {
  // Pick new random color values
  let r = random(255);
  let g = random(255);
  let b = random(255);

  httpPost(
    url,
    'json',
    postData,
    function(result) {
      // ... won't be called
    },
    function(error) {
      strokeWeight(2);
      stroke(r, g, b);
      fill(r, g, b, 127);
      text(error.toString(), mouseX, mouseY);
    }
  );
}

Overloads

path name of the file or url to load

datatype? “json”, “jsonp”, “xml”, or “text”. If omitted, httpPost() will guess.

data? param data passed sent with request

callback? function to be executed after httpPost() 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

data param data passed sent with request

callback? function to be executed after httpPost() 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

callback function to be executed after httpPost() 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