-1

In my code, there is a parent-child relationship between entities. Now I have to write the Rest API to create a child.

I am confused between POST and PUT methods. If I will look into master it is like updation in master so I have to go with the PUT method. If I will look into child it is like the creation of a new child.

Code:

@PostMapping("/master/{masterId}/child/{childId}")
public ResponseEntity<Void> insertChildDetail(

I am using the POST method but I am not sure about it.

Could you please let me know which method is better to use and why so that I can get a better understanding.

Vimit Dhawan
  • 597
  • 1
  • 7
  • 25
  • 2
    According to specification, `POST` is used to create an new resources and `PUT` is used to update existing resources. More at [PUT vs. POST in REST](https://stackoverflow.com/questions/630453/put-vs-post-in-rest) – Sudhir Ojha Sep 17 '19 at 05:38
  • 1
    @SudhirOjha That's not quite right... You should read the answers of the question you linked above. Also have a look at my [answer](https://stackoverflow.com/a/57970140/1426227). Both `POST` and `PUT` methods can be used for creating resources. – cassiomolin Sep 17 '19 at 15:04
  • 1
    @cossiomolin your answer is very helpful for everyone who landed on your answer, I am also one of them. Yes I have given a link in my comments that contains more details than my comment. I generally use POST for creating new resources and PUT for update existing one. That's why I given a comment not as an answer. – Sudhir Ojha Sep 17 '19 at 16:33

3 Answers3

2

Both POST and PUT methods can be used to create resources. If your application generates resource identifiers on behalf of the client, then you should use POST instead of PUT for creating resources.

To support it, I've quoted some parts of the PUT method definition below (highlight is mine):

4.3.4. PUT

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. [...]

If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response. If the target resource does have a current representation and that representation is successfully modified in accordance with the state of the enclosed representation, then the origin server MUST send either a 200 (OK) or a 204 (No Content) response to indicate successful completion of the request. [...]

Proper interpretation of a PUT request presumes that the user agent knows which target resource is desired. A service that selects a proper URI on behalf of the client, after receiving a state-changing request, SHOULD be implemented using the POST method rather than PUT. [...]

Now, find below some relevant quotes on the POST method definition:

4.3.3. POST

The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):

[...]

  • Creating a new resource that has yet to be identified by the origin server;

[...]

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created and a representation that describes the status of the request while referring to the new resource(s).

While the 201 status code indicates that a new resource has been created, the Location header indicate where the newly created resource is located. If no Location header is provided, then the client should assume that the resource is identified by the effective request URI:

6.3.2. 201 Created

The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI. [...]

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

It depends upon the scenario. IMHO, creation is higher priority than update. So POST method is more standard than PUT method.

Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30
vikinam97
  • 26
  • 3
0

PUT would make more sense when looking from the parent Object's perspective.

Parent -> Child

Since parent already exists, the child is an updation to the parent object. So, I'd say PUT would be more appropriate.

POST -> create
PUT -> create/update
iminiki
  • 2,549
  • 12
  • 35
  • 45