Skip to content

Instantly share code, notes, and snippets.

@williamsiuhang
Last active April 1, 2019 00:24
Show Gist options
  • Save williamsiuhang/202c536b67c3c2d57438770345724de6 to your computer and use it in GitHub Desktop.
Save williamsiuhang/202c536b67c3c2d57438770345724de6 to your computer and use it in GitHub Desktop.
async-fetch
// Basic GET request using fetch API
async function GET() {
try{
var url = "http://domain.com/route/api/endpoint";
var get = await fetch(url);
var val = await get.text();
console.log(val);
}catch(e){
console.log(e);
}
}
// Basic POST request
async function POST() {
try{
var url = "http://domain.com/route/api/endpoint"
var data = {
project: "project nam",
date: "20190331",
time: "123123"
}
var post = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: JSON.stringify(data), // body data type must match "Content-Type" header
});
var val = await post.text();
}catch(e){
console.log(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment