POST as GET


#1

Hi,

I have a requirement where a search request is having a complex data type being passed. In such a case, I thought of using POST as a GET. However for the same resource, I also need POST for creating a new resource instance.

In this case, how do I specify 2 POST methods for the same resource?

Thanks and regards,
Ashfaq


#2

2 POST methods for the same resource

Technically, you could achieve this by defining a RAML union type in the request definition of that resource but I think it would may get confusing for people using that API. Why not define two separate resources?


#3

Thanks for your reply, Jonathan.

I wanted to avoid having 2 separate resources simply because the underlying resource is still the same. For example, if we have an API that deals with both retrieval and creation of customer information, I want to have a POST with header X-HTTP-OVERRIDE set as GET for retrieval and POST with no X-HTTP-OVERRIDE header for creation.


#4

One thing I’ve done is to use structures that include an operation name and a structure appropriate to that operation, where the RAML specifies and object like {"search": {...query details here...}} for each operation, and the POST gets a union of the specific operation structures.

That’s fairly clean, and let’s me defined an API that reasonable. Probably not strictly RESTful, but that’s not a goal for my projects.


#5

As JStoiko said you really should be defining two different resources. You can of course do it as you like, but API clarity should be considered over convenience.

As I like to say, it is our job, the developer, to do the extra coding work to ensure the consumers of our APIs find it easy to use. In this case, creating a separate resource for the search would be the right path to take.