Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda