admin管理员组文章数量:1323714
I'm trying to use the Fetch API to post text/plain
:
fetch('/test', {
method: 'post',
headers: {
'Content-Type': 'text/plain'
},
body: "this is a test"
})
.then(response => response.text())
.then(body => {
console.log(body)
})
This is the router that deals with the request
router.post('/test', (req, res) => {
console.log(req.body)
res.send('OK')
})
However, the output of console.log(req.body)
is {}
.
I'm trying to use the Fetch API to post text/plain
:
fetch('/test', {
method: 'post',
headers: {
'Content-Type': 'text/plain'
},
body: "this is a test"
})
.then(response => response.text())
.then(body => {
console.log(body)
})
This is the router that deals with the request
router.post('/test', (req, res) => {
console.log(req.body)
res.send('OK')
})
However, the output of console.log(req.body)
is {}
.
-
Note that it also highly depends on your server framewor. For example, if this is Express then all you need to do is remember to use the
bodyParser.text()
middleware in your route handler, in addition to sending thetext/plain
content type header with your request. – Mike 'Pomax' Kamermans Commented Jun 3, 2024 at 17:59
2 Answers
Reset to default 3Here are a few things you can try to troubleshoot the issue:
Make sure you're using a middleware like body-parser to parse the request body. Without it, the req.body
object will be empty.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// your routes...
You can try using FormData
on the client-side to send the data as a multipart/form-data
instead of text/plain
.
const formData = new FormData();
formData.append('text', 'this is a test');
fetch('/test', {
method: 'post',
body: formData
})
.then(response => response.text())
.then(body => {
console.log(body);
});
If you still want to use text/plain, you can try sending the data as a stringified JSON object
.
fetch('/test', {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'this is a test'
})
})
.then(response => response.text())
.then(body => {
console.log(body);
});
Then on the server-side, you can access the data with req.body.text
.
I hope this helps you solve the issue!
Here, bodyparser.text middleware will only parse the request ing with the type of text/plain
.
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = 3000;
app.use(bodyParser.text({
type: 'text/plain'
}));
// DESC: Route to handle test
// Method: POST
// Access: PUBLIC
app.post('/test', (req, res) => {
console.log(req.body);
res.send('OK');
});
app.listen(PORT, () => console.log(`Server is running at http://localhost:${PORT}`));
本文标签: javascriptHow to use Fetch API to post textplainStack Overflow
版权声明:本文标题:javascript - How to use Fetch API to post textplain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126934a2421984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论