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
actually your code can be improved like this:
to do a post just add the method on the settings of the fetch call.
getDevices = async () => {
const location = window.location.hostname;
const settings = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
};
try {
const fetchResponse = await fetch(`http://${location}:9000/api/sensors/`, settings);
const data = await fetchResponse.json();
return data;
} catch (e) {
return e;
}
}