Skip to content

methodOverride retains stale Content-Length after rewriting a form body #5520

Description

@imharjot

Hono version

4.13.13 (08a023c on main)

Runtime and version

Node.js 24.11.1

Reproduction

import { Hono } from 'hono'
import { methodOverride } from 'hono/method-override'

const app = new Hono()
app.use('/posts', methodOverride({ app }))
app.delete('/posts', async (c) => {
  const body = await c.req.text()
  return c.json({
    body,
    actualBytes: new TextEncoder().encode(body).length,
    contentLength: c.req.header('content-length'),
  })
})

const response = await app.request('/posts', {
  method: 'POST',
  body: '_method=DELETE&message=Hello',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': '28',
  },
})
console.log(await response.json())
// { body: 'message=Hello', actualBytes: 13, contentLength: '28' }

Expected behavior

Removing _method changes the body length. The rewritten request should discard the old Content-Length, as the multipart branch already does.

Actual behavior

The URLencoded branch retains the incoming length. Forwarding this rewritten body and its headers with Node's fetch fails with UND_ERR_REQ_CONTENT_LENGTH_MISMATCH. A local HTTP echo server reproduces the failure; removing the stale header lets fetch calculate the correct length of 13 bytes.

Custom form field names have the same problem. Requests without an override field are unaffected.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions