Search code examples
restapipostapi-design

RESTful API POST call request without a body


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?


Solution

  • 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.