Guide to Common HTTP Status Codes

WBOY
Release: 2024-01-05 16:14:46
Original
691 people have browsed it

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
    Copy after login
  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
    Copy after login
  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
    Copy after login
  4. Returns 403 Forbidden:

    @app.route('/admin')
    def admin():
     if not session.get('is_admin'):
         abort(403)
     # 管理员页面的逻辑处理
     return 'Welcome, admin!', 200
    Copy after login
  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)
    Copy after login
  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
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!