简体   繁体   English

Flask请求和application / json内容类型

[英]Flask request and application/json content type

I have a flask app with the following view: 我有一个带有以下视图的烧瓶应用程序:

@menus.route('/', methods=["PUT", "POST"])
def new():
    return jsonify(request.json)

However, this only works if the request's content type is set to application/json , otherwise the dict request.json is None. 但是,这仅在请求的内容类型设置为application/json ,否则dict request.json为None。

I know that request.data has the request body as a string, but I don't want to be parsing it to a dict everytime a client forgets to set the request's content-type. 我知道request.data将请求主体作为字符串,但我不希望每次客户端忘记设置请求的内容类型时将其解析为dict。

Is there a way to assume that every incoming request's content-type is application/json ? 有没有办法假设每个传入请求的内容类型是application/json All I want is to always have access to a valid request.json dict, even if the client forgets to set the application content-type to json. 我想要的是始终可以访问有效的request.json dict,即使客户端忘记将应用程序内容类型设置为json。

Use request.get_json() and set force to True : 使用request.get_json()并将force设置为True

@menus.route('/', methods=["PUT", "POST"])
def new():
    return jsonify(request.get_json(force=True))

From the documentation: 从文档:

By default this function will only load the json data if the mimetype is application/json but this can be overridden by the force parameter. 默认情况下,如果mimetype是application/json此函数将仅加载json数据,但这可以被force参数覆盖。

Parameters: 参数:

  • force – if set to True the mimetype is ignored. force - 如果设置为True ,则忽略mimetype。

For older Flask versions, < 0.10, if you want to be forgiving and allow for JSON, always, you can do the decode yourself, explicitly: 对于较旧的Flask版本,<0.10,如果您想要宽容并允许使用JSON,您可以自己进行解码:

from flask import json

@menus.route('/', methods=["PUT", "POST"])
def new():
    return jsonify(json.loads(request.data))

the request object already has a method get_json which can give you the json regardless of the content-type if you execute it with force=True so your code would be something like the following: request对象已经有一个方法get_json ,如果你用force=True执行它,它可以为你提供json而不管内容类型如何,所以你的代码将如下所示:

@menus.route('/', methods=["PUT", "POST"])
def new():
    return jsonify(request.get_json(force=True))

in fact, the flask documentation says that request.get_json should be used instead of request.json : http://flask.pocoo.org/docs/api/?highlight=json#flask.Request.json 事实上,烧瓶文件说, request.get_json应该用来代替request.jsonhttp://flask.pocoo.org/docs/api/?highlight=json#flask.Request.json

暂无
暂无

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

相关问题 Flask-Restful 错误:请求 Content-Type 不是 'application/json'。"} - Flask-Restful Error: request Content-Type was not 'application/json'."} 烧瓶中没有POST请求和Content-Type“application / json”的响应 - No response with POST request and Content-Type “application/json” in flask GET 请求不在 json 内容类型 flask - GET request not in json content type flask 根据 Flask 中请求的内容类型更改响应 - Change response based on content type of request in Flask 在视图中强制应用程序/json MIME 类型(Flask) - Forcing application/json MIME type in a view (Flask) 如何从带有jsonapi版本1.0,“ Content-type” =“ application / vnd.api + json”的Flask应用中获取有效的json resp - How to get valid json resp from Flask app with jsonapi version 1.0, “Content-type”=“application/vnd.api+json” Flask - 当内容类型为“application/x-www-form-urlencoded”时,如何在 POST 请求中读取原始正文 - Flask - How do I read the raw body in a POST request when the content type is "application/x-www-form-urlencoded" 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? 在标头中设置Content-Type / application / json - Setting Content-Type / application/json in header 如何验证请求正文是进入 python Flask 应用程序的有效 JSON - How to validate a request body is a valid JSON coming into a python flask application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM