nodejs / nodejs/undici

DispatchInterceptor with POST request doesn't work

Open
#2,209 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Docs
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Bug Description

When a POST request is made using a dispatch interceptor and undici.DecoratorHandler, an error is thrown if the request includes a body. However, if the request does not include a body, no error is thrown.

/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/index.js:109
      Error.captureStackTrace(err, this)
            ^
TypeError: fetch failed
    at fetch (/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/index.js:109:13) {
  cause: TypeError: this.handler.onBodySent is not a function
      at undiciInterceptorTesto.onBodySent (/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/lib/handler/DecoratorHandler.js:33:25)
      at Request.onBodySent (/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/lib/core/request.js:205:24)
      at AsyncWriter.write (/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/lib/client.js:1714:13)
      at writeIterable (/Users/battal/Desktop/files/projects/win-bot/node_modules/undici/lib/client.js:1637:19)
}

Reproducible By

import * as undici from "undici";
import { fetch } from "undici";

const undiciInterceptorHeaders = (dispatch: any) => {
  class undiciInterceptorTesto extends undici.DecoratorHandler {
    onHeaders(statusCode: any, headers: any, resume: any) {
      // @ts-ignore
      return super.onHeaders(statusCode, headers, resume);
    }
  }
  return function InterceptedDispatch(opts: any, handler: any) {
    return dispatch(opts, new undiciInterceptorTesto(handler));
  };
};

let dispatcher = new undici.Agent({
  interceptors: {
    Client: [undiciInterceptorHeaders as unknown as undici.Dispatcher],
  },
});

fetch("https://postman-echo.com/post", {
  method: "POST",
  body: new URLSearchParams("amk=5&asdm=6"),
  // body: new URLSearchParams(""), // works fine
  dispatcher,
})
  .then((res) => res.json())
  .then((body) => {
    console.log(body);
  });

Expected Behavior

it should not throw error

Environment

Macos Sonoma Beta 4
Node v20.5.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/handler/DecoratorHandler.js and follow the onBodySent path into lib/core/request.js and lib/client.js. Run the TypeScript reproducer with a POST body and compare it with the empty-body case. Done means the dispatch interceptor handles the body-bearing POST without throwing the reported onBodySent error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.