【发布时间】:2024-04-15 08:30:02
【问题描述】:
我尝试从 POST 请求中检索纯文本数据,但得到 [object Object] 数据。我已经阅读了很多关于表达未定义问题的内容,但这始终是 json,但我需要纯文本或字符串。好的 json 也用于传递字符串,但我想知道我们是否可以不使用 json,而是使用纯文本。
所以我这样做了:
import express from 'express';
import bodyParser from 'body-parser';
const app = express()
const urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/login', urlencodedParser, function (req, res) {
console.log(req.body)
res.send('welcome, ' + req.body)
})
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
console.log('http://localhost:3000');
});
$ curl -X POST -H 'content-type: plain/text' --data "Hello world!" http://localhost:3000/login
welcome, [object Object]u@h ~/Dropbox/heroku/post-process
$
编辑 我更正了“text/plain”的 curl 命令,但它不起作用
$ curl -X POST -H 'content-type: text/plain' --data "Hello world!" http://localhost:3000/login
welcome, [object Object]u@h ~/Dropbox/heroku/post-process
$
【问题讨论】:
-
How do I ask a good question?:“写一个总结具体问题的标题”
-
内容类型是
text/plain不是plain/text -
感谢这个建议。无论如何它没有帮助
-
标题很好,因为你好世界是我试图得到的
-
“写一个总结具体问题的标题” - “要求”不是问题o.O
标签: javascript node.js express curl post