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

318
Visualizações
Error on trying to override POST method with PUT (Method Not Allowed)

I am experiencing a "Method Not Allowed" issue when trying to override my POST request with PUT (for updating information in my blog). I already installed method override for koa.

HTML:

  <div class="create-message content">
<form action="/messages/edit/<%= message.id %>?_method=PUT" method="POST">
  <label for="title">Message title:</label>
  <input required value="<%= message.title %>" type="text" id="title" name="title" required>
  <label for="snippet">Message snippet:</label>
  <input required value="<%= message.snippet %>" type="text" id="snippet" name="snippet" required>
  <label for="body">Message body:</label>
  <input required value="<%= message.body %>" type="text" id="body" name="body" required>
  <button>Update</button>
</form>
My routs are the following:
  //edit message
  router.get('/messages/edit/:id', async (ctx, next) => {
    const id = ctx.params.id;
    const result = await MessageModel.findById(id)
    await  ctx.render('edit', {
      title: 'Messages',  
      message: result
     })
    });

The code above runs well, but after I click on submit button, "Method Not Allowed" issue occurs instead of running this:

  //update edited message
  router.put('/messages/edit/:id', async (ctx, next) => {
      MessageModel.findByIdAndUpdate(ctx.params.id, ctx.request.body, {new:true}, (err:any, result:any) => {
      })
    return ctx.redirect('/');
});

Please share your thoughts on this issue. Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Make sure you add the following to your app.js (index.js) file.

app.use(methodOverride('_method'));
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't set the form method to PUT, it's either GET or POST.

to send a PUT request you can use AJAX, via (for instance) the Fetch API

EDIT: my bad, you used the "tunneling" concept mentioned in that solution.

per this documentation, maybe try with

<form method="POST" action="/resource" enctype="application/x-www-form-urlencoded"> 
<input type="hidden" name="_method" value="PUT">
about 4 years ago · Juan Pablo Isaza 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