【发布时间】:2022-11-17 05:27:39
【问题描述】:
我有一个名为 data.json 的 json 文件,我希望它的内容成为 postData 函数中的 POST 请求的主体。
我如何读取内容或将内容作为我在 nodeJS 中的 const body 变量的值?
数据.json
{"name":"John", "age":30, "car":null}
索引.js
function postData() {
**const body = [];**
const headers = {
"Content-type": "application/json",
};
axios
.post(`${BASE_URL}/data`, body, { headers })
.then((response) => {
if (response.status === 200) {
console.log("Your Data has been saved!");
}
})
.catch((e) => {
console.error(e);
});
}
postData();
【问题讨论】:
标签: javascript node.js rest axios