Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GET or POST  RSS feed

 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an endpoint that will determine if a passed in integer is prime or not. Should the request to this endpoint be GET or POST? Right now, I'm thinking it should be GET since it doesn't do anything to any resource. Thanks
 
subodh kureel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most commonly , GET is use for retrieve purpose . If there is need to update the resource POST method can be used.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it "does something" to resources is an often-made error when determining this. The question should really be: is the request idempotent or not?
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, it's ok to use GET to update resource, so long it's idempotent? if so, what's the difference with PUT? thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PUT is also idempotent (no matter how many times you PUT the same thing, the result is the same). The point is, are you getting a resource or putting a resource? You need to simply look at the intent of the request. Somehow the whole "change the database" as a factor got mixed in there and it shouldn't be.

David Spades wrote:so, it's ok to use GET to update resource, so long it's idempotent?

So, no.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spades wrote:I have an endpoint that will determine if a passed in integer is prime or not.


It's a little bit difficult trying to think of that as a "resource", which perhaps is the problem. To me it's intuitively obvious that getting an answer to the question of whether a particular integer is prime should be a GET, but I find it very hard to explain that in terms of the thing that answers that question being a "resource".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no problem seeing it as a resource. Certain inputs are fed in, and a deterministic and idempotent response is returned. It's the classic definition of a resource GET.

The fact that the response payload is computed is a red herring -- it could just as well (conceptually) be held in a humongous table that contained all possible answers.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's a standard GET situation. But I suppose to me a "resource" is a thing and not a process -- although I can't explain why I have that connotation in my head.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way: it's not a process. There is a URI that addresses a value. The URI addresses that same value at all times. The fact that the value is the result of a process is hidden behind the API. The API is a black box -- it simply maps URIs to value.

Again, it could be an (impossible) infinite table with a boolean column for every possible numeric value that serves as the primary key -- no process involved.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so with PUT or POST, is it ok to return the affected resource too as part of response? or should server only return the response code and the link to the affected resource?
but then again, if we return the resource too, then it would be the same as GET?
thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be fine.
 
subodh kureel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys for my understanding on this subject...

ok, so with PUT or POST, is it ok to return the affected resource too as part of response? or should server only return the response code and the link to the affected resource?


There is fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI,it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.


Please advise, if I am missing anything here...

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!