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

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

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

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

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.

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:

确定