nextjs api_error ai_generated true

API Route body not parsed / req.body is undefined

ID: nextjs/api-route-body-not-parsed

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
14 active

Root Cause

Request body not parsed in API route. Content-Type header missing or wrong handler setup.

generic

Workarounds

  1. 95% success In App Router, use request.json() to parse body: const body = await request.json()
    const body = await request.json()

    Sources: https://nextjs.org/docs/app/building-your-application/routing/route-handlers

  2. 88% success Ensure client sends Content-Type: application/json header
    fetch('/api/route', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) })

    Sources: https://nextjs.org/docs/app/building-your-application/routing/route-handlers

Dead Ends

Common approaches that don't work:

  1. Install body-parser middleware 75% fail

    Next.js has built-in body parsing — external middleware is unnecessary

Error Chain

Frequently confused with: