I\'m working on a project that requires me to make requests to an API. What is the proper form for making a POST request with Async/Await?
As an example, here is my
Remember to separate async/await
and then
here is an example:
const addDevice = async (device) => {
const { hostname: location } = window.location;
const settings = {
method: 'POST',
body: JSON.stringify(device),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}
const response = await fetch(`http://${location}:9000/api/sensors/`, settings);
if (!response.ok) throw Error(response.message);
try {
const data = await response.json();
return data;
} catch (err) {
throw err;
}
};