Is it possible to use the same resource name for get and post rest api

Once upon a time, I developed the Restful service in Java with only 1 GET resource. It was available as follows:

GET http: // localhost: 8080 / my-project / customers / transactions

This GET request returns all client transactions.

Now I have another project request where they want to insert client transactions into another schema in the same database. I thought, instead of creating another service, I could improve this service, since the basic database is the same about transactions with clients.

So, I created another method in my createCustomerTransactions service interface, and I think to call it the same as my GET request, but this one will be POST as follows:

POST http: // localhost: 8080 / my-project / customers / transactions

I tested this with Soap-UI and it works. My question is the right way to do Restful. Is it possible for both GET and POST to have the same URL inside, although they will point to different actual methods? I am poorly versed in names, so I cannot find another better name for the resource.

+5
source share
2 answers

Renovated when used with HTTP depends on resources (URLs) and relies on the actions of HTTP verbs, this is common, and good practice has used these verbs to define some operations on resources that you have:

  • GET retrieves all or just one resource.
  • Normally POST creates a new resource.
  • PUT used to update a resource
  • DELETE delete a resource

    One of the first tasks that we must complete before launching our Restful API is to determine what resources we need and what their attributes will be. The first rule over this approach is to use nouns rather than verbs such as a person, ticket, client, etc.

After defining your resources, you need to determine what actions are applicable to them and how they will be displayed in your API. RESTful principles provide strategies for handling CRUD actions using HTTP methods, displayed as follows.

GET / tickets - Get a list of tickets

GET / tickets / 12 - Get a specific ticket

POST / tickets - Creates a new ticket

PUT / tickets / 12 - Update ticket No. 12

PATCH / tickets / 12 - Partially updates ticket number 12 <- checks this approach.

DELETE / tickets / 12 - Deletes ticket # 12

The above depends on the firewall configurations, but consider the above as a suggestion for API design principles.

+5
source

Yes, you can. This is actually one of the basic foundations of a RESTful desgin. Its not crud / RPC e.g. createTransaction or fetchTransaction. HTTP verbs are used to indicate actions on resources.

0
source

Source: https://habr.com/ru/post/1214995/


All Articles