HTTP

HTTP 201 Created vs 401 Unauthorized

HTTP 201 (Created) is a 2xx Success response, while 401 (Unauthorized) is a 4xx Client Error response. 201 indicates that the request succeeded and a new resource was created. Typically returned after POST or PUT requests that create a new entity. In contrast, 401 means that the request requires user authentication. The response includes a WWW-Authenticate header indicating the authentication scheme.

描述

The request succeeded and a new resource was created. Typically returned after POST or PUT requests that create a new entity.

何时出现

After successfully creating a new user, post, order, or other resource via a REST API.

如何修复

No fix needed. Check the Location header for the URL of the new resource.

描述

The request requires user authentication. The response includes a WWW-Authenticate header indicating the authentication scheme.

何时出现

When accessing a protected resource without credentials or with expired tokens.

如何修复

Include valid authentication credentials (API key, Bearer token, Basic auth) in the Authorization header.

主要区别

1.

201 is a 2xx Success response, while 401 is a 4xx Client Error response.

2.

HTTP 201: The request succeeded and a new resource was created. Typically returned after POST or PUT requests that create a new entity.

3.

HTTP 401: The request requires user authentication. The response includes a WWW-Authenticate header indicating the authentication scheme.

4.

You encounter 201 when after successfully creating a new user, post, order, or other resource via a REST API.

5.

You encounter 401 when when accessing a protected resource without credentials or with expired tokens.

何时使用哪个

For 201 (Created): No fix needed. Check the Location header for the URL of the new resource. For 401 (Unauthorized): Include valid authentication credentials (API key, Bearer token, Basic auth) in the Authorization header.

了解更多