简体   繁体   English

为什么我无法使用add_argument()将HTTP标头发送到Flask-RESTful reqparse模块?

[英]Why am I unable to send HTTP Headers to Flask-RESTful reqparse module using add_argument()?

I am trying to integrate the Flask-RESTful's request parsing interface, reqparse in my backend to request HTTP headers from the client. 我正在尝试集成Flask-RESTful的请求解析接口,在我的后端reqparse来请求来自客户端的HTTP头。 Currently, I wish to use this for authentication of a user and wish to pass 'secret_key' in HTTP headers. 目前,我希望使用它来验证用户并希望在HTTP标头中传递'secret_key'

The function I am using for this task is the add_argument() function. 我用于此任务的函数是add_argument()函数。 My code for requesting the header is as follows: 我请求标题的代码如下:

reqparse = reqparse.RequestParser()
reqparse.add_argument('secret_key', type=str, location='headers', required=True)

However, on sending the following cURL request: 但是,在发送以下cURL请求时:

curl -H "Content-Type: application/json" -H "{secret_key: SECRET}" -X POST -d '{}' http://localhost:5000/authUser

I recieve the following 400 error on my Pycharm Community edition editor : 我在Pycharm社区版编辑器上收到以下400错误:

127.0.0.1 - - [02/Aug/2016 18:48:59] "POST /authUser HTTP/1.1" 400 -

and the following message on my cURL terminal: 以及我的cURL终端上的以下消息:

{
  "message": {
    "secret_key": "Missing required parameter in the HTTP headers"
  }
}

To reproduce this error on Pycharm (and hopefully all other compilers as well), please use the files written below as follows: 要在Pycharm上重现此错误(并希望所有其他编译器也是如此),请使用下面编写的文件,如下所示:

Folder - Sample_App
    - __init__.py
    - run.py
    - views.py

__init__.py __init__.py

from flask import Flask
from flask_restful import Api
from views import AuthUser

app = Flask(__name__)

api = Api(app)
api.add_resource(AuthUser, '/authUser')

views.py views.py

from flask_restful import reqparse, Resource

class AuthUser(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('secret_key', type=str, location='headers', required=True)

    def post(self):
        data = self.reqparse.parse_args()
        if data['secret_key'] == "SECRET":
            print("Success")
            return 200
        return 400

run.py run.py

from __init__ import app

app.run(host='0.0.0.0', port=5000, debug=True)

Could you please tell me how to fix this issue? 你能告诉我如何解决这个问题吗? I want to know if the location parameter needs to be changed or if there is something wrong in my cURL request. 我想知道是否需要更改location参数或者我的cURL请求中是否有错误。

EDIT: 编辑:

With the help of Methika's response, I have figured out the error. 在Methika的回应的帮助下,我发现了错误。 The add_argument() function does not take _ in a headers parameter. add_argument()函数不在headers参数中使用_ However, when I use the requests.headers['secret_key'] function, I am able to request headers with the _ character just fine. 但是,当我使用requests.headers['secret_key']函数时,我可以请求带有_字符的标题。 Why is that the case? 为什么会这样?

New Code of views.py : 新的views.py代码:

views.py views.py

from flask_restful import reqparse, Resource

class AuthUser(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()

    def post(self):
        data = self.reqparse.parse_args()
        data['secret_key'] = request.headers['secret_key']
        if data['secret_key'] == "SECRET":
            print("Success")
            return 200
        return 400

I did some tests with the code you gave here and I found out that the problem doesn't come from you code but from the name of the variable: 我用你在这里给出的代码做了一些测试,我发现问题不是来自你的代码,而是来自变量的名称:

If you replace secret_key by secretkey (or something else without underscore) it will work ! 如果更换secret_key通过secretkey (或别的东西,没有下划线),它的工作!

I found this post , flask seems to not accept underscores in header variable names. 我发现这个帖子 ,烧瓶似乎不接受标题变量名称中的下划线。

Instead of this curl request 而不是这个卷曲请求

curl -H "Content-Type: application/json" -H "{secret_key: SECRET}" -X POST -d '{}' http://localhost:5000/authUser

Try this one 试试这个吧

curl -H "Content-Type: application/json" -H "secret_key: SECRET" -X     POST -d '{}' http://localhost:5000/authUser

In header normally I have seen using value like "Key: Value" 在标题中我通常看到使用像“Key:Value”这样的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM