Last active
April 1, 2019 00:24
-
-
Save williamsiuhang/202c536b67c3c2d57438770345724de6 to your computer and use it in GitHub Desktop.
async-fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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