Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Created December 13, 2018 22:32
Show Gist options
  • Save kitsune7/7ebc490d86b9550ddb5d7a8c2119f072 to your computer and use it in GitHub Desktop.
Save kitsune7/7ebc490d86b9550ddb5d7a8c2119f072 to your computer and use it in GitHub Desktop.
Functions for making a GET or POST request on the client.
async function post (url, data) {
let request = {
body: JSON.stringify(data),
method: 'post',
headers: { 'Content-Type': 'application/json' }
}
return fetch(url, request)
.then(async (response) => {
return response.json()
})
.catch((error) => {
console.log('Fetch error:', error)
return null
})
}
async function get (url) {
let request = {
method: 'get',
headers: {
'Content-Type': 'application/json'
}
}
return fetch(url, request)
.then(response => response.json())
.catch((error) => {
console.log('Fetch error:', error)
return null
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment