You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express PUT接口中req.body返回Object导致文件写入类型错误的问题求助

Express PUT接口中req.body返回Object导致文件写入类型错误的问题求助

我现在碰到个棘手的问题:req.body 返回的是一个Object类型,运行下面的代码时直接触发了TypeError,感觉req变量里根本没拿到我传的有效内容。

首先说下我用来测试的curl命令,我想把字符串here-again写入到/home/dave/test.txt文件里,试过给here-again加引号,但还是没用:

curl -X PUT http://localhost:8008/config/ --data here-again

这是我的Node.js代码:

app.put('/config/',  function (req, res)  {
    const filename = '/home/dave/test.txt';
    const data = req.body;
    //res.send('body ' + data + ' ' + filename );
    console.log(data)
    fs.writeFile(filename, data, (err) => {
    if (err) {
        console.error(err);
        res.status(500).send('Error writing file');
    } else {
        res.send('Data saved successfully');
    }
  });
})

最后是我收到的错误信息:

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object

备注:内容来源于stack exchange,提问作者D Liebman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:29:36