X-HTTP-Method-Override 在 Flask 的 Python Web 服务器中

huangapple go评论118阅读模式
英文:

X-HTTP-Method-Override in Flask's Python Web server

问题

I'm learning HTTP and I try to learn how different headers work. I want to implement a simple HTTP server using Flask and Python just to test the X-HTTP-Method-Override header.

My code:

  1. class HTTPMethodOverrideMiddleware(object):
  2. allowed_methods = frozenset(
  3. ["GET", "HEAD", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"]
  4. )
  5. bodyless_methods = frozenset(["GET", "HEAD", "OPTIONS", "DELETE"])
  6. def __init__(self, app):
  7. self.app = app
  8. def __call__(self, environ, start_response):
  9. method = environ.get("HTTP_X_HTTP_METHOD_OVERRIDE", "").upper()
  10. if method in self.allowed_methods:
  11. environ["REQUEST_METHOD"] = method
  12. if method in self.bodyless_methods:
  13. environ["CONTENT_LENGTH"] = "0"
  14. return self.app(environ, start_response)
  15. from flask import Flask, request
  16. app = Flask(__name__)
  17. @app.route("/")
  18. def index():
  19. return request.method
  20. app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
  21. app.run(host="0.0.0.0", port=9090)

When I'm sending this curl request: curl -X GET "http://127.0.0.1:9090" server returns GET, which is OK, but with this: curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override : POST" it still shows GET instead of POST.

What is wrong? The code or curl?

英文:

I'm learning HTTP and I try to learn how different headers work. I want to implement a simple HTTP server using Flask and Python just to test theX-HTTP-Method-Override header.

My code:

  1. class HTTPMethodOverrideMiddleware(object):
  2. allowed_methods = frozenset(
  3. ["GET", "HEAD", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"]
  4. )
  5. bodyless_methods = frozenset(["GET", "HEAD", "OPTIONS", "DELETE"])
  6. def __init__(self, app):
  7. self.app = app
  8. def __call__(self, environ, start_response):
  9. method = environ.get("HTTP_X_HTTP_METHOD_OVERRIDE", "").upper()
  10. if method in self.allowed_methods:
  11. environ["REQUEST_METHOD"] = method
  12. if method in self.bodyless_methods:
  13. environ["CONTENT_LENGTH"] = "0"
  14. return self.app(environ, start_response)
  15. from flask import Flask, request
  16. app = Flask(__name__)
  17. @app.route("/")
  18. def index():
  19. return request.method
  20. app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
  21. app.run(host="0.0.0.0", port=9090)

When I'm sending this curl request: curl -X GET "http://127.0.0.1:9090" server returns GET, which is OK, but with this: curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override : POST" it still shows GET instead of POST.

What is wrong? The code or curl?

答案1

得分: 1

问题出现在代码和curl命令中。

您需要允许路由上的方法,像这样:

  1. @app.route("/", methods=["GET", "POST"])
  2. def index():
  3. return request.method

这将允许路由上的 GETPOST 方法,使您能够执行以下命令:

  1. curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: POST"
  1. curl -X POST "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: GET"

您在 --header 中的 : 之间有空格,导致它无法正确解析。

英文:

The issue is in both code and curl command.

You need to allow the methods on the route, like this:

  1. @app.route("/", methods = ["GET", "POST"])
  2. def index():
  3. return request.method

This will allow GET and POST methods on the route, enabling you to execute the following commands:

  1. curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: POST"
  1. curl -X POST "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: GET"

You had spaces between : in the --header which caused it not parsing corrently.

huangapple
  • 本文由 发表于 2023年7月13日 15:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定