flask-restful(python)无法解析表单数据

时间:2015-12-28 14:52:29

标签: python json

我正在使用 Python Flask-Restful 来发布帖子请求。我使用 PostMan(Chrome)来测试我的apis。我在邮递员的标题部分将ContentType设置为 application / json 。我只能以原始值的形式获取参数,当我更改为表单数据时,我收到错误消息'浏览器(或代理)发送了此服务器无法理解的请求。'

(使用原始的参数) enter image description here

enter image description here

(使用 form-data 的参数) enter image description here

这是我的代码:



# -*- coding: UTF-8 -*-
from app import app, db, models, api, DataModels
from flask.ext import restful
from flask.ext.restful import reqparse
from flask import jsonify, request

class SchoolListHandler(restful.Resource):

    def post(self):
        json_data = request.get_json(force=True)
        name = json_data['name']
        slogan = json_data['slogan']
        print "name is: %s, slogan is: %s" % (name, slogan)

        return jsonify(result="xxxx")

api.add_resource(SchoolListHandler, "/api/allSchools")




另外,我尝试使用reqparse来获取我的参数,但问题仍未解决:



# -*- coding: UTF-8 -*-
from app import app, db, models, api, DataModels
from flask.ext import restful
from flask.ext.restful import reqparse
from flask import jsonify, request

class SchoolListHandler(restful.Resource):
    def get(self):
        all_schools = DataModels.School.School.query.all()
        return jsonify(data=[x.json for x in all_schools])

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("name", type=unicode, required=True, location="json")
        parser.add_argument("slogan", type=unicode, required=True, location="json")
        args = parser.parse_args()
        name = args['name']
        slogan = args['slogan']
        
        return jsonify(result="xxxx")

api.add_resource(SchoolListHandler, "/api/allSchools")




那么,我怎么能解决这个问题,提前谢谢!

2 个答案:

答案 0 :(得分:0)

您是否检查了请求中的内容? 您可以使用以下方法从表单数据中获取值:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.1.2/foundation.js"></script>
<button type="button" onclick="closeAll()"> Close All </button>
<ul class="accordion accroot" data-accordion data-allow-all-closed='true'>
  <li class="accordion-item is-active" data-accordion-item>
    <a href="#" class="accordion-title">Accordion 1</a>
    <div class="accordion-content" data-tab-content>
      Panel 1. Lorem ipsum dolor
    </div>
  </li>
  <li class="accordion-item" data-accordion-item>
    <a href="#" class="accordion-title">Accordion 2</a>
    <div class="accordion-content" data-tab-content>
      Panel 2. Lorem ipsum dolor
    </div>
  </li>
  <li class="accordion-item" data-accordion-item>
    <a href="#" class="accordion-title">Accordion 3</a>
    <div class="accordion-content" data-tab-content>
      Panel 3. Lorem ipsum dolor
    </div>
  </li>
</ul>

答案 1 :(得分:0)

在第二个示例中,当使用reqparse时,您将表单数据的位置指定为location =“ json”。如果执行此操作,则将忽略form-data,因为reqparse只会在不存在的json正文中搜索该参数,并立即抛出错误,因为这是必需的。

使用refparse可能是正确的方法,尽管imo处理表单数据通常很麻烦。