How do I handle both GET and POST requests (Non-API) like how PHP handles both?
Answered
Barbary Lion posted this in #help-forum
Barbary LionOP
My guess is to use a route to receive a request, then forward a page with it, but I have no idea how to actually do it.
Answered by joulev
You can’t. The POST method is reserved for server actions. As a user you can only handle the GET method.
To handle arbitrary methods, you must use route handlers or middleware
To handle arbitrary methods, you must use route handlers or middleware
5 Replies
I dont get what you mean by handling both types of REST requests and it being non-api?
What actually are you trying to do?
@Barbary Lion My guess is to use a route to receive a request, then forward a page with it, but I have no idea how to actually do it.
You can’t. The POST method is reserved for server actions. As a user you can only handle the GET method.
To handle arbitrary methods, you must use route handlers or middleware
To handle arbitrary methods, you must use route handlers or middleware
Answer
Barbary LionOP
@Clown @joulev
To clarify, I want to display the page based on the request. For a GET request to the same URL, I want to show "pizza.js". Similarly, for a POST request to the same URL, I want to display "pizzaRecipe.js".
So, when a GET request is received, I'll send an X component, and when a POST request is received, I'll send a Y component.
To clarify, I want to display the page based on the request. For a GET request to the same URL, I want to show "pizza.js". Similarly, for a POST request to the same URL, I want to display "pizzaRecipe.js".
So, when a GET request is received, I'll send an X component, and when a POST request is received, I'll send a Y component.
Well, you have the answer above: you simply can’t. Nextjs doesn’t support that