Created
January 5, 2023 15:42
-
-
Save gioiliop7/0a7a4a2f762fb9a69ac2efd13c2870c8 to your computer and use it in GitHub Desktop.
Fetch api call [JS]
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
export async function apiCall(body, method, url) { | |
let headers = new Headers(); | |
headers.append("Content-Type", "application/json"); | |
const requestOptions = { | |
method: method, | |
headers: headers, | |
body: JSON.stringify(body), | |
redirect: "follow", | |
}; | |
try { | |
const response = await fetch(url, requestOptions); | |
const data = await response.json(); | |
return data; | |
} catch (error) { | |
console.error(error); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment