Flask 请求语法错误的问题

发布于 2025-01-20 00:05:35 字数 1102 浏览 2 评论 0原文

我正在测试/试图学习烧瓶,并且正在flast_restful。我得到的此问题是:

代码400,消息不良请求语法('name = testItem')

main.py.py

from flask import Flask,request
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

product_put_args = reqparse.RequestParser()
product_put_args.add_argument("name", type = str, help = "Name of the product")
product_put_args.add_argument("quantity", type = int, help = "Quantity of the item")

products = {}

class Product(Resource):
    def get(self, barcode):
        return products[barcode]

    def put(self, barcode):
        args = product_put_args.parse_args()
        return {barcode: args}

api.add_resource(Product, "/product/<int:barcode>")

if(__name__) == "__main__":
    app.run(debug = True)

和我的 test.py

import requests

base = "http://127.0.0.1:5000/"

response = requests.put(base + "product/1", {"name": "testitem"})
print(response.json())

我试图改革两个文件,以找出要发送问题的内容,我觉得这很简单,但是如果您可以帮助我,我敢打赌,这将有所帮助我和许多其他试图开始创建REST API的人。

I am testing/attempting to learn flask, and flast_restful. This issue I get is:

code 400, message Bad request syntax ('name=testitem')

main.py:

from flask import Flask,request
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

product_put_args = reqparse.RequestParser()
product_put_args.add_argument("name", type = str, help = "Name of the product")
product_put_args.add_argument("quantity", type = int, help = "Quantity of the item")

products = {}

class Product(Resource):
    def get(self, barcode):
        return products[barcode]

    def put(self, barcode):
        args = product_put_args.parse_args()
        return {barcode: args}

api.add_resource(Product, "/product/<int:barcode>")

if(__name__) == "__main__":
    app.run(debug = True)

and my
test.py

import requests

base = "http://127.0.0.1:5000/"

response = requests.put(base + "product/1", {"name": "testitem"})
print(response.json())

I have attempted to reform mat and change around both files to figure out what is sending the issue, I feel like it is something simple, but if you can help me, I bet this will help me and many others that are trying to start creating a rest API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2025-01-27 00:05:35

您需要默认情况下将位置信息添加到RequestParser中,它尝试从flask.request.valuesflask.request.json.json中解析值,但是在您的情况下,这些值需要从a flask.request.request.form 。下面的代码解决了您的错误

from flask import Flask,request
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

product_put_args = reqparse.RequestParser()
product_put_args.add_argument("name", type = str, help = "Name of the product", location='form')
product_put_args.add_argument("quantity", type = int, help = "Quantity of the item", location='form')

products = {}

class Product(Resource):
    def get(self, barcode):
        return products[barcode]

    def put(self, barcode):
        args = product_put_args.parse_args()
        products[barcode] = args['name']
        return {barcode: args}

api.add_resource(Product, "/product/<int:barcode>")

if(__name__) == "__main__":
    app.run(debug = True)

You need to add the location information to the RequestParser by default it tries to parse values from flask.Request.values, and flask.Request.json, but in your case, the values need to be parsed from a flask.request.form. Below code fixes your error

from flask import Flask,request
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

product_put_args = reqparse.RequestParser()
product_put_args.add_argument("name", type = str, help = "Name of the product", location='form')
product_put_args.add_argument("quantity", type = int, help = "Quantity of the item", location='form')

products = {}

class Product(Resource):
    def get(self, barcode):
        return products[barcode]

    def put(self, barcode):
        args = product_put_args.parse_args()
        products[barcode] = args['name']
        return {barcode: args}

api.add_resource(Product, "/product/<int:barcode>")

if(__name__) == "__main__":
    app.run(debug = True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文