h3 icon indicating copy to clipboard operation
h3 copied to clipboard

An error occurred when using the proxyRequest method to proxy a DELETE request.

Open WongSaang opened this issue 2 years ago • 5 comments

Environment

Nuxt

Reproduction

This is the code for the server middleware in my Nuxt project:

export default defineEventHandler((event) => {
    if (event.node.req.url.startsWith('/api/')) {
        return proxyRequest(
            event,
            (process.env.SERVER_DOMAIN || 'http://localhost:8000') + event.node.req.url
        )
    }
})

Describe the bug

[nuxt] [request error] [unhandled] [500] fetch failed                                                                                         16:59:26
  at Object.fetch (node:internal/deps/undici/undici:14062:11)
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at async sendProxy (./node_modules/h3/dist/index.mjs:506:20)
  at async Object.handler (./node_modules/h3/dist/index.mjs:1247:19)
  at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1322:7)

Additional context

No response

Logs

No response

WongSaang avatar Apr 12 '23 09:04 WongSaang

I had the same problem

lenqwang avatar Apr 14 '23 14:04 lenqwang

Found a fix from https://github.com/unjs/nitro/issues/1137

export NODE_OPTIONS='--no-experimental-fetch'

It seems the experimental fetch doesn't like content-length: 0 for deletes.

tjhiggins avatar Apr 20 '23 19:04 tjhiggins

My workaround for this issue was to add an empty body in the DELETE method.

fetch(url, { method: 'DELETE', body: [] }),

migueabellan avatar Jan 24 '24 11:01 migueabellan

(note for myself) some nice investigation by @jasenmichael https://github.com/unjs/h3/pull/605#issuecomment-1910787078 most intrestingly that it seems to be reproducable only with Nuxt3 (maybe because of browser request?)

pi0 avatar Jan 25 '24 22:01 pi0

We had this problem and the problem to proxy OPTIONS requests. It seems that from Node v20.11.0 onwards, this issue has been fixed within the fetch as upgrading node from v18.7.1 to v20.11.0 removes the error.

The problem here was originated from undici's fetch which resulted the error UND_ERR_REQ_CONTENT_LENGTH_MISMATCH. For some reason, the error the h3 server prints does not include the cause. To print that we needed to modify these lines to catch original error: https://github.com/unjs/h3/blob/c8aa150b59d12f1a25ef154c00eeae132da59a32/src/utils/proxy.ts#L84C3-L88C6

const response = await _getFetch(opts.fetch)(target, {
    headers: opts.headers,
    ignoreResponseError: true,
    // make $ofetch.raw transparent
    ...opts.fetchOptions
  }).catch((error) => {
    console.log(error.message, error)
    throw error
  });
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at async sendProxy (file:///****/node_modules/h3/dist/index.mjs:1096:20)
    at async file:///****/node_modules/h3/dist/index.mjs:1700:19
    at async Object.callAsync (file:///****/node_modules/unctx/dist/index.mjs:72:16)
    at async Server.toNodeHandle (file:///****/node_modules/h3/dist/index.mjs:1910:7) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (node:internal/deps/undici/undici:10059:41)
      at _resume (node:internal/deps/undici/undici:10037:33)
      at resume (node:internal/deps/undici/undici:9938:7)
      at connect (node:internal/deps/undici/undici:9927:7) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }
}

eljass avatar Feb 23 '24 10:02 eljass

Is there a permanent solution being worked on for this? @pi0

I evaluated @migueabellan 's suggestion for a quick solution.

ramazansancar avatar Feb 17 '25 13:02 ramazansancar

This issues seems to be fixed in latest Node.js versions from multiple reports.

If after upgrading to latest LTS version of Node still facing issues please ping.

pi0 avatar Apr 15 '25 12:04 pi0

Upgrading established projects to the latest version of node can be difficult and time consuming. @pi0

Making a permanent solution to this issue specifically for this library will make things easier for many people.

ramazansancar avatar Apr 16 '25 07:04 ramazansancar

@pi0 I'm seeing this issue arise again

------------------------------
- Operating System: Darwin
- Node Version:     v22.14.0
- Nuxt Version:     4.1.3
- CLI Version:      3.29.3
- Nitro Version:    2.12.6
- Package Manager:  [email protected]
- Builder:          -
- User Config:      -
- Runtime Modules:  -
- Build Modules:    -
------------------------------

adamdehaven avatar Oct 30 '25 16:10 adamdehaven