4

I want to create a new call in my API that links two already created resources together. Therefore, I don't need to pass any json entities in the post body I just need the resources IDs which I am passing in the URL. Is that a wrong practice? so basically my request now is only a simple path {cid}/projects/{projectID}/subcontractors/{subcontractorID} and in the post call method I extract the resources IDs from the path and I link them. The response is only either pass or fail {"success":true}. Is that a wrong practice? Is there a better way of doing this?

2 Answers 2

2

How you will design your API is really up to you. From a technical point of view, a POST request with an empty payload is completely fine.


However, assuming that you intend to add a contractor to a project, I think it could be better expressed with a payload:

POST /projects/1/contractors HTTP/1.1 Host: api.example.org Content-Type: application/json { "contractorId": 100 } 

This approach is specially useful if you need to manage more information for that contractor in that project. If above request succeeds, the response would contain the 201 status code along with a Location header that identifies the newly created resource.

Sign up to request clarification or add additional context in comments.

3 Comments

but I already have another post call for creating the contractor similar to your example but with few extra fields: POST /projects/1/contractors Host: api.example.org Content-Type: application/json { "ContarctorName":"Contractor 1", "ContarctorAddress":"Address 123", } So the new linking POST call has to be different.
@ZuzuJH I would use POST /contractors to create contractors and POST /projects/{id}/contractors to linking the contractors to a project.
I do apologies for not explaining my issue fully. so our system has two level company level and project level. therefore we have a call for adding & linking the subcontractor at the company level and we have a call for for adding & linking the subcontractor at the project level. The call that I am trying to achieve now is just for linking the contractors that are already existing at the company level to the project level. I hope that makes sense now.
2

Since you are linking the already existing resources - projects and contractors. I wouldn't prefer the POST method. Instead I would use the PATCH method (as I am only editing the partial content of the existing resources) Either the payload or the request URL methods are acceptable.

Request URL:

PATCH /projects/3/contractors/23 HTTP/1.1 HOST example.com/api 

Payload

PATCH /projects/3/contractors HTTP/1.1 HOST example.com/api Content-Type: application/json { "contractor_id": 23 } 

A successful response is indicated by 200 status code which may contain the payload, or 204 response

1 Comment

Thank you so much. I never knew about this http method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.