我正在做一个需要我向API发出请求的项目。用Async / Await发出POST请求的正确形式是什么?
举例来说,这是我获取所有设备列表的方法。如何将请求更改为POST以创建新设备?我知道我必须添加带有数据主体的标头。
getDevices = async () => {
const location = window.location.hostname;
const response = await fetch(
`http://${location}:9000/api/sensors/`
);
const data = await response.json();
if (response.status !== 200) throw Error(data.message);
return data;
};
Run Code Online (Sandbox Code Playgroud)