I'm trying to understand if the intention behind my request affects whether I should use GET or POST when making fetch requests. I know that when sending a JSON payload, it has to be a POST request. However, I've also heard that POST should be used when the request is meant to change something on the server, while GET is for simply fetching data. So, if I have a basic endpoint, like mysite.com/endpoint, that starts a process on the server without needing a JSON payload, should that be a POST request instead?
5 Answers
The key thing to remember is that GET requests should be idempotent. This means you can run the same request multiple times without changing the state. If you trigger something with a GET, it should be safe even if the request is repeated. If you have something like starting a long-running process, it's better suited for a POST request instead.
It's crucial to separate conventions from rules that impact functionality. For instance, browsers treat GET requests as safe and cacheable, meaning they can be called multiple times without concern. If you use GET for actions, it might cause unintended repeated calls due to caching mechanisms.
Thinking of it in terms of the CRUD operations helps:
- **GET** (Read): Used to fetch data; typically, less info is sent in the request, often just through the URL.
- **POST** (Create): Used to create new resources, which usually involves sending a lot of information.
- **PUT** (Update): Used to edit existing resources; most people use POST for this if it’s a new or existing item.
- **DELETE**: Removes a specified item, which is commonly handled through URL parameters.
For your question about toggling a process, I’d suggest considering it more like a PUT since it's changing the state.
In my opinion, GET requests should never change any state or trigger processes. Basically, a GET request should be safe to repeat without side effects.
If you want to be RESTful, follow the standard HTTP methods as outlined in documentation. You can check out this link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods for more info.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically