0

I am developing login api for my android app and website. But I am not sure if both api should be same or different. For website, after successful login, the page should redirect to another page. If the redirection is from server side, then I cannot use it for android app.

I am using Spring framework on server side.

Can anyone suggest, what should be the best approach and how other website and their mobile apps work?

binay37
  • 121
  • 1
  • 11
  • Or should you add an extra parameter to check what platform it is. Example you login using desktop, and now in your api request include the paltform parameter to check weather if it's coming from mobile or not. Then change a bit your server side code, if mobile just return true or false if not mobile include the redirection. Something like that, but I am sure there's a better way to do it. Just my thought though. –  Aug 19 '16 at 08:42

4 Answers4

1

The api for mobile and web should be same for optimized way. as you mention on the web redirection from server it could also manage on mobile but you have to send some signal with response from api that where it redirect.

Mohit Dixit
  • 176
  • 1
  • 8
  • In Spring MVC, I can use @ResponseBody for return data in case of REST api, but how can I use the same if I have to redirect? – binay37 Aug 19 '16 at 09:23
  • @binay37 you can take help from this http://stackoverflow.com/questions/17955777/redirect-to-an-external-url-from-controller-action-in-spring-mvc – Mohit Dixit Aug 19 '16 at 09:28
  • It is only for redirection. But I am not getting how can I return json data using the same api when required instead of redirecting. – binay37 Aug 19 '16 at 09:42
  • @binay37 in that case try this https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/ – Mohit Dixit Aug 19 '16 at 09:52
  • I think making separate api for both will be better as the request and response methods will be different but internally both can use the same code. For website, it will return HTML data or will redirect to different page whereas for android app it will return JSON data. – binay37 Aug 19 '16 at 09:56
  • @binay37 it's depend upon your requirements,but in most of the cases they should be same for every platform because then it is easy to maintain them with efficiency. and for return data json is prefer to use. – Mohit Dixit Aug 19 '16 at 10:07
  • I will make separate only in the cases where redirection is required from server side. Because, I found some posts saying that redirection and returning json data in different cases from same method is not possible. Separating both means, only it will differ in request and response. – binay37 Aug 19 '16 at 10:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121335/discussion-between-mohit-dixit-and-binay37). – Mohit Dixit Aug 19 '16 at 10:18
0

When you have different clients that need to react differently on the very same server response (i.e. your login), you should leave it to the client to decide whether to redirect or not. Otherwise your backend would have a latent dependency on the clients, which is likely a hint at a bad design.

PS.: Of course, if your client does not know about where it should redirect, you should put possible links/options into your response. The actual decision, however, is still done by your client.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
0

ah,app api is different from pc api,we don't control that session, because,I don't know the network.so ,general,we design a token,put the httpRequest in side.

My english is just so so !!

Stephen.lin
  • 166
  • 1
  • 12
0

I think making separate api for both will be better as the request and response methods will be different but internally both can use the same code.

For website, it will return HTML data or will redirect to different page whereas for android app it will return JSON data.

Thanks Mohit. I can use single api as given in the link:
Spring MVC either sending JSON or Redirecting to another page

Community
  • 1
  • 1
binay37
  • 121
  • 1
  • 11
  • 1
    it's depend upon your requirements,but in most of the cases they should be same for every platform because then it is easy to maintain them with efficiency. – Mohit Dixit Aug 19 '16 at 10:04