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

NextResponse.rewrite is not forwarding the request body to external hosts in dev when headers are modified in middleware #50200

Closed
1 task done
florianliebig opened this issue May 23, 2023 · 11 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.

Comments

@florianliebig
Copy link

florianliebig commented May 23, 2023

Verify canary release

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

Provide environment information

Reproducable in canary with a jest test. Occurs beginning from Next 13.3 and all versions after that.

ONLY AN ISSUE IN DEV MODE

Which area(s) of Next.js are affected? (leave empty if unsure)

Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue or a replay of the bug

#49606

To Reproduce

Create a middleware endpoint and Rewrite the response to an external API. Copy and modify the headers accordingly to https://vercel.com/templates/next.js/edge-functions-modify-request-header

if (url.pathname.includes('/middleware-external-rewrite-body-headers')) {
    const tmpHeaders = new Headers(request.headers)

    tmpHeaders.set('x-hello-from-middleware1', 'hello')

    return NextResponse.rewrite(
      'https://next-data-api-endpoint.vercel.app/api/echo-body',
      {
        request: {
          headers: tmpHeaders,
        },
      }
    )
}

Make an API call with a body (in this case json-data) in dev mode:

const res = await next.fetch( '/middleware-external-rewrite-body-headers', { redirect: 'manual', method: 'POST', body, } )

Check the body you receive in the external target

Describe the Bug

#48040 fixed the body rewrite to external resources. But as soon as you modify the headers in your middleware, still no body is received at the target:

` expect(received).toEqual(expected) // deep equality

Expected: "{\"hello\":\"world\"}"
Received: ""`

#49606

I was also able to locally reproduce using ngrok.

Expected Behavior

The request body should always be rewritten fully in every case.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next dev

@florianliebig florianliebig added the bug Issue was opened via the bug report template. label May 23, 2023
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label May 23, 2023
@sl1mpshady
Copy link

+1

@exallium
Copy link

+1 this is preventing me from being able to adopt later versions.

@gnoyixiang
Copy link

seems to be working for me in v13.4.7

@robfarb
Copy link

robfarb commented Jun 27, 2023

+1. I upgraded from next v13.4.4 -> v13.4.7 and I'm still experiencing this issue. When I log the req.text() in the next middleware I can see the entire request payload, but when I log the request payload from the django-rest-framework endpoint that I'm calling, the request body is empty.

@florianliebig
Copy link
Author

florianliebig commented Jun 28, 2023

I updated the failing test branch to latest canary: #49606

Still failing the test:

pnpm testonly test/e2e/middleware-rewrites -t "should handle middleware rewrite with body and headers correctly"

Error:

    Expected: "{\"hello\":\"world\"}"
    Received: ""

@catchshyam
Copy link

catchshyam commented Sep 10, 2023

@florianliebig I think this is not a bug. Can you confirm that you are passing Content-Type header? For me, the issue is reproducible if you don't explicitly pass that header. But if I do, it works just fine. I am using the edge function in the middleware

@saeedghasemi72
Copy link

@florianliebig I think this is not a bug. Can you confirm that you are passing Content-Type header? For me, the issue is reproducible if you don't explicitly pass that header. But if I do, it works just fine. I am using the edge function in the middleware

Oh, thanks, man! This help was a lifesaver. I spent a week on it.

@florianliebig
Copy link
Author

@florianliebig I think this is not a bug. Can you confirm that you are passing Content-Type header? For me, the issue is reproducible if you don't explicitly pass that header. But if I do, it works just fine. I am using the edge function in the middleware

Doesn't help for me. Issue is actually that the body stream is invalid which I was able to check with ngrok. Maybe thats not an issue for some backends if you don't have anything in body

@saeedghasemi72
Copy link

@florianliebig I think this is not a bug. Can you confirm that you are passing Content-Type header? For me, the issue is reproducible if you don't explicitly pass that header. But if I do, it works just fine. I am using the edge function in the middleware

Doesn't help for me. Issue is actually that the body stream is invalid which I was able to check with ngrok. Maybe thats not an issue for some backends if you don't have anything in body

after setting the "Content-Type" , "application/json" in request, middleware rewrite and next.config rewrite worked correctly for me.

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label Mar 18, 2025
@nextjs-bot
Copy link
Collaborator

This issue has been automatically closed due to two years of inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

@nextjs-bot nextjs-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

8 participants