http - How should Transient Resources be retrieved in a RESTful API -
for while (wrongly) thinking restful api exposed crud operation persisted entities web application. when code in "the real world" find out not enough. example, bank account transfer doesn't have persisted entity. transient resource post
/transfers/
, in payload specify details:
{"accounttocredit":1234, "accounttodebit":5678, "amount":10}
using post
here makes sense because changes state on server ($10 moves 1 account every time post
occurs).
what should happen in case doesn't affect server? simple first answer use get
. example, want list of savings , checking accounts have less $100. call get
/accounts/searchresults?minbalance=0&maxbalance=100
. happens though if search parameter need use complex objects wouldn't fit in maximum length of get
request.
my first thought use post
, after thinking more should put
since isn't changing state of server, (limited) understanding though of put
updating resource , post
creating resource (like creating search results). should used in case?
i found following links provide information wasn't clear me should used in different cases:
transient rest representations
i agree approach, seems reasonable me use get
when searching resources, , said in 1 of provided links, the whole point of query strings doing things search. agree put
fits better when want update resource in idempotent way (no matter how many times hit request, result same).
so generally, propose. now, if limited maximum length of get
request, use post
or put
, passing parameters in json, in uri like:
put /api/search
you see "search resource" send new parameters. know seems workaround , may worried rest avoiding verbs in uris. well, there few cases it's still acceptable , restful use verbs, e.g. in cases calculation or conversion involved generate result (for more this, check this reference).
ps. think workaround still restful, if wasn't, rest isn't obsession , ultimate goal. being pragmatic , keeping clean api design might better approach, if in few cases not restful.
Comments
Post a Comment