Skip to content

Instantly share code, notes, and snippets.

@zshell31
Last active July 1, 2016 10:09
Show Gist options
  • Save zshell31/79791da5e0eb08f82e743f0e764734be to your computer and use it in GitHub Desktop.
Save zshell31/79791da5e0eb08f82e743f0e764734be to your computer and use it in GitHub Desktop.
Json Server Api calls
function api(method, uri, body) {
url = 'http://localhost:3000/' + uri;
return fetch(url, {
method: method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(function(response) {
return response.json()
})
.then(function(json) {
console.log('parsed json: ', json)
})
.catch(function(ex) {
console.log('parsing failed: ', ex)
});
}
function get(uri) { return api('GET', uri); }
function post(uri, body) { return api('POST', uri, body); }
function put(uri, body) { return api('PUT', uri, body); }
function del(uri) { return api('DELETE', uri); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment