Home > Mobile >  Should I use GET or POST for an endpoint passing or returning anything in Spring Boot?
Should I use GET or POST for an endpoint passing or returning anything in Spring Boot?

Time:01-19

I have a mail sender method in my Spring Boot app and when I defining related endpoint in the Controller, I could not be sure what is the most proper request for that.

As I do not pass any parameter and the method does not return any content, I am not sure POST or GET is suitable for this. So, which request should I use?

CodePudding user response:

Premised that the choice depends on your personal opinion and habit as developer, and on the specific purpose of your application.

However, in your specific case, I would follow the below logic:

  1. Request is about "retrieving" email -> GET method is better
  2. Request is about "sending" email -> PUT method is better

So the logic is the following: as long as I ask the server to only "retrieve" information and I am not going to send any information to the server (i.e: coming from a form), i will always use GET. On the other hand, when you also need to pass info to the server and it needs to apply some logic/operation, which might also affect some database, in that case the POST method works better.

Hope that answer your questions. Feel free to add more details, I will strive to help you further.

CodePudding user response:

You need to think of the intention behind the request. Since it is a MAIL request, you are intending to perform some action with this. Hence it would be advisable to use the POST method.

Here is a reference to the existing methods:

GET : The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

POST : The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.

Here is the Link for the MDN docs for this.

When you think about the future, there might be some data that you need to send for MAIL which you might not need now, hence using POST makes most sense.

  • Related