Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
When I post data with fetch post, I don't receive data

I have a problem with fetch post, I want to send data to an url but it doesn't work..

function TodoTaskForm () {
    const taskContentInput = useRef(null)
    const handleSubmit = async (e) => {
        e.preventDefault()
        fetch('/api/tasks', {
            method: 'POST',
            body: JSON.stringify({content: taskContentInput.current.value})
        })
    }

    return (
        <form onSubmit={handleSubmit} className="__component_todolist_form_container">
            <input type="text" name="task" ref={taskContentInput} placeholder="nouvelle tâche.."></input>
        </form>
    )
}

In my component, I'm doing this and in my express server :

app.post('/api/tasks', (req, res) => {
    console.log(req.body)
    console.log('request received!')
})

When I test, i receive the request but req.body return "{}" in my console, I don't understand, im using app.use(express.json()) but it doesn't work, I have even try to use body-parser but... So please, I need help.. thank you!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need:

  1. A body parser which matches the data being send. You've switched from sending form encoded data to sending JSON. Note that Express has built-in body parsing middleware and does not need the separate body-parse NPM module.
  2. A Content-Type header on the request which states what format the data is in so the correct body parser can be triggered.

Such:

app.post('/api/tasks', express.json(), (req, res) => {
    console.log(req.body)
    console.log('request received!')
})

and

fetch('/api/tasks', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({content: taskContentInput.current.value})
})
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!