Correct http status code for resource which requires authorization

后端 未结 8 706
太阳男子
太阳男子 2021-01-29 23:51

There seems to be a lot of confusion about the correct http status code to return if the user tries to access a page which requires the user to login.

So basically what

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 00:35

    Agreed. REST is just a style, not a strict protocol. Many public web services deviate from this style. You can build your service to return whatever you want. Just make sure your clients know how what return codes to expect.

    Personally, I have always used 401 (unauthorized) to indicate an unauthenticated user has requested a resource that requires a login. I then require the client application to guide the user to the login.

    I use 400 (bad request) in response to a logon attempt with invalid credentials.

    HTTP 302 (moved) seems more appropriate for web applications where the client is a browser. Browsers typically follow the re-direct address in the response. This can be useful for guiding the user to a logon page.

提交回复
热议问题