AlwaysLearning
AlwaysLearning

Reputation: 2875

RESTful Web Api chain Do I use POST or GET?

If I am calling a Web APi service and that service makes various other calls to other services, do I use POST or GET?

To elaborate further, let's say I call Web Api Service One saying 'Do the thing'. Web Api One's job when requested thus, is to GET data from Service Two and POST data to Service Three. Perhaps Service One will then update Service Two. Service One will then respond to caller with any success/failure.

My question again is should I the caller, use POST or GET to Service One?

Upvotes: 0

Views: 259

Answers (2)

cassiomolin
cassiomolin

Reputation: 130917

It's all about the semantics of the request. From the RFC 7231:

The request method token is the primary source of request semantics; it indicates the purpose for which the client has made this request and what is expected by the client as a successful result.

Here's a brief description of some HTTP methods defined in the RFC RFC 7231 (click the links to check the full method definition):

  • GET: Transfer a current representation of the target resource.
  • HEAD: Same as GET, but only transfer the status line and header section.
  • POST: Perform resource-specific processing on the request payload.
  • PUT: Replace all current representations of the target resource with the request payload.
  • DELETE: Remove all current representations of the target resource

In addition to the methods listed above, the RFC 5789 standardized the PATCH HTTP method for performing partial updates to a resource.

POST is commonly seen as a "catch all" method, once the target resource process the request payload according to the resource's own specific semantics.


HTTP methods can be classified as safe and/or idempotent and it must be taken into account when designing an API with on the top of HTTP.

Upvotes: 1

Oliver McPhee
Oliver McPhee

Reputation: 1056

Typically I would only use a variety of HTTP verbs (GET, POST, PUT, DELETE etc) when using a REST API. Then the endpoints are resources in their own right. For example:

/car

So the verbs make sense (are you getting a car? creating one? updating one? removing one?)

But when I am not using a REST API the HTTP verbs tend to have less meaning and I generally only use HTTP POST. Otherwise you hit logical problems like you're experiencing here.

eg

/rentacar

This API models an RPC that may cause many resources to change in many ways. It is not a REST API and so the HTTP Verbs don't really relate.

But in honesty, for RPC (remote procedure calls), people choose between GET and POST by either:

  1. GET for operations that don’t modify anything and POST for other cases.
  2. GET for operations that don’t need too much parameters and POST for other cases.
  3. GET and POST on a random basis or always use POST.

Purists prefer 1. But sometimes you don't know when modifications are going to occur. So, take your pick!

Upvotes: 0

Related Questions