Skip to content

Instantly share code, notes, and snippets.

@gioiliop7
Created January 5, 2023 15:42
Show Gist options
  • Save gioiliop7/0a7a4a2f762fb9a69ac2efd13c2870c8 to your computer and use it in GitHub Desktop.
Save gioiliop7/0a7a4a2f762fb9a69ac2efd13c2870c8 to your computer and use it in GitHub Desktop.
Fetch api call [JS]
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