Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST Statelessness and user session in web services

I want to develop a REST API. REST guidelines specify that the state mustn't be stored on the server side. But the REST methods I want to implement imply user connection management. In order to respect the statelessness, do I need to give the user credentials in each REST method request ? I find that quite inefficient... Isn't there an other more effective way ??

like image 363
ayorosmage Avatar asked Dec 29 '22 03:12

ayorosmage


1 Answers

Statelessness is one of the main constrains of a REST architecture, as can be read in the original publication:

5.1.3 Stateless

We next add a constraint to the client-server interaction: communication must be stateless in nature, as in the client-stateless-server (CSS) style of Section 3.4.3 (Figure 5-3), such that each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client.

So for the credentials you mentioned, you should provide them in each call separately (i.e. Basic Auth + SSL). Of course, this is were "the real world" comes in, and applications start to differ. You might also use OAuth, fixed tokens, etc. but remember that you're then weakening the "RESTfulness" of your API.

like image 77
b_erb Avatar answered Jan 03 '23 01:01

b_erb