Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When fetching a Request object in middleware, the method is ignored #37123

Closed
1 task done
ambrauer opened this issue May 23, 2022 · 4 comments · Fixed by #37540
Closed
1 task done

When fetching a Request object in middleware, the method is ignored #37123

ambrauer opened this issue May 23, 2022 · 4 comments · Fixed by #37540
Labels
Middleware Related to Next.js Middleware

Comments

@ambrauer
Copy link

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
Binaries:
  Node: 16.14.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.1.7-canary.11
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

n/a

How are you deploying your application? (if relevant)

next start

Describe the Bug

When fetching a Request object in middleware, the method is ignored:

const request = new Request('http://example.com', { method: 'POST', body: 'hello'})
const response = await fetch(request)

expect(response.req.method).toEqual('POST') // <-- return 'GET'

This is related to #36522 (case 2 here, which I believe was lost in the shuffle).

Expected Behavior

Specified 'method' should be used for the request

To Reproduce

A simple repro we have in a middleware.ts:

import { NextResponse } from 'next/server';

export default async function () {
  const ok = await fetch('https://httpbin.org/post', {
    method: 'POST',
    body: 'test',
  });
  console.log(ok); // <-- 200 OK

  const fails = await fetch(
    new Request('https://httpbin.org/post', {
      method: 'POST',
      body: 'test',
    })
  );
  console.log(fails); // <-- 405 METHOD NOT ALLOWED (GET is sent)

  return NextResponse.next();
}
@ambrauer ambrauer added the bug Issue was opened via the bug report template. label May 23, 2022
@ijjk
Copy link
Member

ijjk commented May 29, 2022

Hi, this should be fixed by #36539 which is available in v12.1.7-canary.19 of Next.js, please update and give it a try!

@ijjk ijjk closed this as completed May 29, 2022
@ambrauer
Copy link
Author

Hi @ijjk, thanks for the update although I'm still seeing the issue with 12.1.7-canary.19 (and I also tried 12.1.7-canary.23, latest canary I had trouble quickly setting up same repro). The mentioned fix only applies to request cloning. Seems like this issue should be re-opened?

@balazsorban44
Copy link
Member

I believe this was reintroduced in #37024

@balazsorban44 balazsorban44 reopened this Jun 8, 2022
@balazsorban44 balazsorban44 added type: needs investigation Middleware Related to Next.js Middleware and removed bug Issue was opened via the bug report template. labels Jun 8, 2022
@kodiakhq kodiakhq bot closed this as completed in #37540 Jun 8, 2022
kodiakhq bot pushed a commit that referenced this issue Jun 8, 2022
…37540)

There was a bug that ignored `Request` options when one was given to the `fetch` function:

```ts
const request = new Request("https://example.vercel.sh", { method: "POST" });
await fetch(request);
```

The code above was expected to make a `POST` request, but instead it
made a `GET` request.

This commit fixes it and adds some tests to verify that fetching with a
`Request` object works as expected, and therefore resolves #37123.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
@github-actions
Copy link
Contributor

github-actions bot commented Jul 8, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Middleware Related to Next.js Middleware
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants