curious mind
curious mind

Reputation: 73

Next.js app api router can parse the incoming request body under post request, but not under get request

I am developing an API using next.js app router. I was able to parse the data using const res = await request.json() when the HTTP request type was post. But I am not able to do so when the HTTP request is get.

⨯ SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:6571:19) at successSteps (node:internal/deps/undici/undici:6545:27) at specConsumeBody (node:internal/deps/undici/undici:6551:9) at NextRequest.json (node:internal/deps/undici/undici:6442:18) at GET (webpack-internal:///(rsc)/./src/app/api/submit-lead-by-get-method/route.ts:12:31) Does anyone know what could be causing the problem and how to fix it?

I am kinda stuck here as per my understanding when sending same body of data to an endpoint it should be able to parse the request no matter what is the HTTP method of the endpoint.

Upvotes: 0

Views: 628

Answers (1)

grekier
grekier

Reputation: 3697

GET request are not meant to send a body. In theory, I believe the HTTP spec do not forbid it but I don't believe most client and server libraries support it.

GET is meant to retrieve data, not send it. If you need parameters, send parameters with http://example.com/api?param1=value1&param2=value2

Upvotes: 1

Related Questions