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 {}.

Share Improve this question edited Nov 30, 2024 at 3:54 Ry- 225k56 gold badges492 silver badges498 bronze badges asked Apr 20, 2023 at 6:51 Sean.LSean.L 531 silver badge6 bronze badges 1
  • 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 the text/plain content type header with your request. – Mike 'Pomax' Kamermans Commented Jun 3, 2024 at 17:59
Add a ment  | 

2 Answers 2

Reset to default 3

Here 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