英文:
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:
class HTTPMethodOverrideMiddleware(object):
allowed_methods = frozenset(
["GET", "HEAD", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"]
)
bodyless_methods = frozenset(["GET", "HEAD", "OPTIONS", "DELETE"])
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
method = environ.get("HTTP_X_HTTP_METHOD_OVERRIDE", "").upper()
if method in self.allowed_methods:
environ["REQUEST_METHOD"] = method
if method in self.bodyless_methods:
environ["CONTENT_LENGTH"] = "0"
return self.app(environ, start_response)
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def index():
return request.method
app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
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:
class HTTPMethodOverrideMiddleware(object):
allowed_methods = frozenset(
["GET", "HEAD", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"]
)
bodyless_methods = frozenset(["GET", "HEAD", "OPTIONS", "DELETE"])
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
method = environ.get("HTTP_X_HTTP_METHOD_OVERRIDE", "").upper()
if method in self.allowed_methods:
environ["REQUEST_METHOD"] = method
if method in self.bodyless_methods:
environ["CONTENT_LENGTH"] = "0"
return self.app(environ, start_response)
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def index():
return request.method
app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
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命令中。
您需要允许路由上的方法,像这样:
@app.route("/", methods=["GET", "POST"])
def index():
return request.method
这将允许路由上的 GET
和 POST
方法,使您能够执行以下命令:
curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: POST"
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:
@app.route("/", methods = ["GET", "POST"])
def index():
return request.method
This will allow GET
and POST
methods on the route, enabling you to execute the following commands:
curl -X GET "http://127.0.0.1:9090" --header "X-HTTP-Method-Override: POST"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论