Home  >  Article  >  Web Front-end  >  Guide to Common HTTP Status Codes

Guide to Common HTTP Status Codes

WBOY
WBOYOriginal
2024-01-05 16:14:46709browse

Guide to Common HTTP Status Codes

HTTP status code setting guide

Introduction:
HTTP (Hypertext Transfer Protocol) is a protocol used to transmit hypertext. It passes between the client and the server. Communicate between requests and responses. During the HTTP communication process, the server will return a status code to indicate the processing result of the request. The correct setting of status codes is crucial to ensure normal network communication. This article will introduce the basic concepts of HTTP status codes and provide some status code setting examples in common scenarios.

1. Classification of HTTP status codes:
The first number of the HTTP status code indicates the five types of responses:
1xx: Informational status code (Informational)
2xx: Success Status code (Successful)
3xx: Redirection status code (Redirection)
4xx: Client error status code (Client Error)
5xx: Server error status code (Server Error)

2. Common HTTP status codes and their meanings:

  1. 200 OK: The request was successful. This status code indicates that the server successfully processed the request and returned the requested resource.
  2. 301 Moved Permanently: Permanent redirection. This status code indicates that the requested resource has been permanently moved to a new URI and future requests should use the new URI.
  3. 302 Found: Temporary redirection. This status code indicates that the requested resource has been temporarily moved to a new URI and future requests should use the original URI.
  4. 400 Bad Request: Bad request. This status code indicates that the server cannot understand the request, usually because the request contains incorrect syntax or parameters.
  5. 403 Forbidden: Access is prohibited. This status code indicates that the server understood the request, but denied access to the requested resource.
  6. 404 Not Found: Resource not found. This status code indicates that the server cannot find the requested resource.
  7. 500 Internal Server Error: Server internal error. This status code indicates that the server encountered an unexpected error and was unable to complete the request.

3. HTTP status code setting example:

  1. Return 200 OK:

    @app.route('/')
    def index():
     return 'Hello, World!', 200
  2. Return 301 Moved Permanently:

    @app.route('/old_url')
    def old_url():
     return redirect(url_for('new_url'), code=301)
    
    @app.route('/new_url')
    def new_url():
     return 'This is the new URL', 200
  3. Returns 400 Bad Request:

    @app.route('/login', methods=['POST'])
    def login():
     if not request.json or 'username' not in request.json:
         abort(400)
     # 其他逻辑处理
     return 'Login successful!', 200
  4. Returns 403 Forbidden:

    @app.route('/admin')
    def admin():
     if not session.get('is_admin'):
         abort(403)
     # 管理员页面的逻辑处理
     return 'Welcome, admin!', 200
  5. Returns 404 Not Found:

    @app.route('/user/<username>')
    def user_profile(username):
     # 根据username查询用户信息
     if not user_exists(username):
         abort(404)
     # 用户信息展示页面的逻辑处理
     return render_template('user_profile.html', username=username)
  6. Returns 500 Internal Server Error:

    @app.route('/validate')
    def validate():
     # 一些验证逻辑
     try:
         # 验证过程中可能引发的异常
         if not validate_something():
             raise Exception('Validation failed')
     except Exception as e:
         app.logger.error(str(e))
         abort(500)
     # 其他逻辑处理
     return 'Validation completed!', 200

Conclusion:
By correctly setting the HTTP status code, the server Ability to better communicate with clients and convey the results of request processing. In actual development, rational selection and setting of HTTP status codes based on business scenarios and needs will help improve user experience and system maintainability.

The above is the detailed content of Guide to Common HTTP Status Codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn