Simple Fetch-like API for Espruino

The fetch module provides a wrapper around http to provide a simple promise-based page getter.

This module loads the entire webpage into RAM, so while in most cases it'll work fine, for very big queries you may still need to fall back to http to handle the data in a streaming manner.

Initialisation

const http = require('http');
const fetch = require('fetch.js')(http);

OR

const fetch = require('fetch.js')() //defaults to using 'http' module

(eventually, using 'tls' module instead of 'http' will be supported, hopefully)

Usage

fetch(url, params).then(response => response.text()).then(text=>{
//do stuff with text
// e.g. console.log(text)
});

url should look like: 'http://google.com/whatever/you/want?foo=bar' params are optional, but should look like:

const params = { method: 'GET'(default) or 'PUT' or 'POST' or 'DELETE', body: String //(for PUT/POST) ...any other params that you'd supply to http.request() }

Working examples:

fetch('http://google.com').then(r=>r.text()).then(console.log);
fetch('http://example.com', {
  method: 'POST', 
  body:JSON.stringify({answer:42}), 
  headers:{'Content-Type':'application/json'}
}).then(r=>r.text()).then(console.log);

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.