Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda