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

POST request handled as GET in nextjs route handlers #13908

Closed
3 tasks done
rodolfoBee opened this issue Oct 8, 2024 · 5 comments · Fixed by #14084
Closed
3 tasks done

POST request handled as GET in nextjs route handlers #13908

rodolfoBee opened this issue Oct 8, 2024 · 5 comments · Fixed by #14084
Assignees
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK

Comments

@rodolfoBee
Copy link
Member

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.33.1

Framework Version

No response

Link to Sentry event

https://dev-curumas.sentry.io/issues/5968125190/?project=5591101&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&statsPeriod=7d&stream_index=0

Reproduction Example/SDK Setup

No response

Steps to Reproduce

Repro: next-test-sentry-main.zip
Follow the steps in the readme:

  1. npm install
  2. update DSN in the sentry config files
  3. npm run dev
  4. send a POST request to http://localhost:3000/api/test

Expected Result

The event in sentry has the POST request information including body.

Actual Result

The request in the Sentry event is treated as a GET request, even though the event is associated with the transaction POST /api/test (http.server)

Image

@github-actions github-actions bot added the Package: nextjs Issues related to the Sentry Nextjs SDK label Oct 8, 2024
@lforst
Copy link
Member

lforst commented Oct 8, 2024

Thanks for raising. For the next person reading this. We need to start filling the request interface properly: https://develop.sentry.dev/sdk/event-payloads/request/

@haveneersrobin
Copy link

haveneersrobin commented Oct 8, 2024

I have worked around this issue in our code by adding an event processor which gets a clone of the request so .json() can be called once more. This can fail if there is no body, so that is something to keep in mind. Roughly looks like this, but I can imagine there is a better way 😄

type EventProcessorCreator = (
  httpRequest: NextRequest,
  status_code: number,
) => Promise<(event: Sentry.Event, hint: Sentry.EventHint) => Promise<Sentry.Event>>;

export const getRequestEventProcessor: EventProcessorCreator =
  async (httpRequest, status_code) => async (event, hint) => {
    const { request, contexts, breadcrumbs } = event;
    const {
      url,
      method,
      cookies,
      nextUrl: { searchParams, pathname },
    } = httpRequest;

    return {
      ...event,
      request: {
        ...request,
        url,
        query_string: Object.fromEntries(searchParams),
        method: method,
        data: await httpRequest.json(),
        cookies: Object.fromEntries(cookies.getAll().map(({ name, value }) => [name, value])),
      },
      contexts: {
        ...contexts,
        response: {
          ...(contexts?.response && { ...contexts.response }),
          status_code,
        },
      },
      transaction: `${method} ${pathname}`,
      breadcrumbs: breadcrumbs?.map(value => ({
        ...value,
        message: value.message ? stripAnsi(value.message) : '',
      })),
    };
  };

// ...

const eventProcessor = await getRequestEventProcessor(request, status);
Sentry.withScope(scope => {
    // ...
    scope.addEventProcessor(eventProcessor);
  });

Setting event.contexts.response directly gave TS errors

@malay44
Copy link
Contributor

malay44 commented Oct 12, 2024

I attempted to reproduce the issue. Although the error was logged on the dashboard, I was unable to view the request section. You can check out the event details here.

To investigate further, I captured the event object being sent from the Next.js server to Sentry using the beforeSend function:

import * as Sentry from "@sentry/nextjs";
import { writeFile } from "fs";

Sentry.init({
  dsn: "<DNS>",
  tracesSampleRate: 1,
  debug: false,
  beforeSend(...args) {
    writeFile("sentryEvent.json", JSON.stringify(args), () => {});
    return args[0];
  },
});

You can find the sentry-event.json file here. However, this event doesn't seem to include the request object.

@lforst
Copy link
Member

lforst commented Oct 14, 2024

@malay44 we can repro the issue. We just need to fix it. It's medium prio for us atm.

Copy link
Contributor

A PR closing this issue has just been released 🚀

This issue was referenced by PR #14084, which was included in the 8.36.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants