Created
January 23, 2020 13:22
-
-
Save akhyaruu/436b39bd4709165b5a94fc8f8fdcbcba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const request = new Request; | |
const jsonData = { | |
id: 42, | |
title: 'Meaning of Life', | |
}; | |
const getJsonResponse = request.get("https://httpbin.org/get"); | |
getJsonResponse.then(data => data).then(data => console.log(data)); | |
const postJsonResponse = request.post("https://httpbin.org/post", jsonData); | |
postJsonResponse.then(data => data).then(data => console.log(data)); | |
const putJsonResponse = request.put("https://httpbin.org/put"); | |
putJsonResponse.then(data => data).then(data => console.log(data)); | |
const deleteJsonResponse = request.delete("https://httpbin.org/delete"); | |
deleteJsonResponse.then(data => data).then(data => console.log(data)); |
This file contains hidden or 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
class Request { | |
async get(url) { | |
const response = await fetch(url); | |
const responseData = response.json(); | |
return responseData; | |
} | |
async delete(url) { | |
const response = fetch(url, { | |
method: 'DELETE', | |
headers: { | |
'Content-type': 'application/json' | |
}, | |
}) | |
const responseData = await 'Resource deleted...'; | |
return responseData; | |
} | |
async put(url, dataBaru) { | |
const response = await fetch(url, { | |
method: 'PUT', | |
headers: { | |
'Content-type': 'application/json' | |
}, | |
body: JSON.stringify(dataBaru) | |
}) | |
const responseData = await response.json(); | |
return responseData; | |
} | |
async post(url, dataBaru) { | |
const response = await fetch(url, { | |
method: 'POST', | |
headers: { | |
'Content-type': 'application/json' | |
}, | |
body: JSON.stringify(dataBaru) | |
}) | |
const responseData = await response.json(); | |
return responseData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment