Zuzu JH
Zuzu JH

Reputation: 627

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?

Upvotes: 4

Views: 12512

Answers (2)

gksingh
gksingh

Reputation: 315

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

Upvotes: 2

cassiomolin
cassiomolin

Reputation: 130907

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.

Upvotes: 2

Related Questions