Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Flask RESTful中有选择地使用marshal_和on-GET响应_Python_Flask_Flask Restful - Fatal编程技术网

Python 在Flask RESTful中有选择地使用marshal_和on-GET响应

Python 在Flask RESTful中有选择地使用marshal_和on-GET响应,python,flask,flask-restful,Python,Flask,Flask Restful,我正在使用Flask RESTful开发一个API。使用此API,我希望能够选择GET enpoint是否必须返回JSON或HTML 当我在没有任何额外变量的情况下调用/shippings/ready时,我希望返回一个JSON对象,其中包含所有状态设置为“ready”的装运。这没有任何问题。 但我也希望能够从Javascript中调用同一个端点,并给它一个format=html标志,在这种情况下,端点必须返回相同的数据,但作为使用Jinja2模板的html字符串 我通过查看request.arg

我正在使用Flask RESTful开发一个API。使用此API,我希望能够选择GET enpoint是否必须返回JSON或HTML

当我在没有任何额外变量的情况下调用/shippings/ready时,我希望返回一个JSON对象,其中包含所有状态设置为“ready”的装运。这没有任何问题。 但我也希望能够从Javascript中调用同一个端点,并给它一个format=html标志,在这种情况下,端点必须返回相同的数据,但作为使用Jinja2模板的html字符串

我通过查看request.args并查看是否设置了格式标志来实现这一点,请参见下面的代码。但我还想使用@marshal_with格式化JSON响应。当我使用这个修饰符时,HTML响应不再适用于JSON对象中的字段(如在字段中指定的字段),而是返回所有设置为null的字段。 我可以通过将@marshal_与移动到一个单独的函数来创建一个变通方法,但我无法想象这是最好的方法

有人知道该怎么做吗

装运单\u字段={ “id”:字段。整数, 等 } @带装运字段的封送 def封送结果: 返回结果 按状态资源分类装运: def getself,状态: 发货=模型.发货.查询.过滤器\按状态=状态.all 如果要求HTML try:try,在“格式”不存在时防止错误 if request.args['format']='html': 返回render_templatepage_snippets/shipping.html\ 出货量=出货量,200 除: 通过 装运=编组装运装运 退货 api.add_ResourceShippings_by_status,'/Shippings/'
定义一个单独的函数来封送数据是一个好主意。但默认情况下,响应内容类型是application/json。要生成html响应,如果Accept标头在请求中包含text/html,则必须通过调用make_response函数生成响应对象

您不必传递查询字符串来请求html输出,但您的请求接受头应该包括text/html,这是浏览器的默认值

def get(self, status):
    shipments = models.Shipment.query.filter_by(status=status).all()

    accept = request.headers.get('Accept')
    if accept and 'text/html' in accept:
            return make_reponse(render_template("page_snippets/shipment.html", shipments=shipments), 200)
    return marshal_shipments(shipments)
如果不希望选中request Accept标头以呈现不同媒体类型的正确输出,则必须为每个媒体类型定义单独的函数,并向api.representation decorator注册它们

@api.representation('text/html')
def output_html(data, code, headers=None):
    resp = make_reponse(render_template("page_snippets/shipment.html", shipments=data), 200)
    resp.headers.extend(headers or {})
    return resp

@api.representation('application/json')
def output_json(data, code, headers=None):
    resp = make_response(json.dumps(data), code)
    resp.headers.extend(headers or {})
    return resp

class Shipments_by_status(Resource):
    @marshal_with(shipments_fields)
    def get(self, status):
        return models.Shipment.query.filter_by(status=status).all()

定义一个单独的函数来封送数据是一个好主意。但默认情况下,响应内容类型是application/json。要生成html响应,如果Accept标头在请求中包含text/html,则必须通过调用make_response函数生成响应对象

您不必传递查询字符串来请求html输出,但您的请求接受头应该包括text/html,这是浏览器的默认值

def get(self, status):
    shipments = models.Shipment.query.filter_by(status=status).all()

    accept = request.headers.get('Accept')
    if accept and 'text/html' in accept:
            return make_reponse(render_template("page_snippets/shipment.html", shipments=shipments), 200)
    return marshal_shipments(shipments)
如果不希望选中request Accept标头以呈现不同媒体类型的正确输出,则必须为每个媒体类型定义单独的函数,并向api.representation decorator注册它们

@api.representation('text/html')
def output_html(data, code, headers=None):
    resp = make_reponse(render_template("page_snippets/shipment.html", shipments=data), 200)
    resp.headers.extend(headers or {})
    return resp

@api.representation('application/json')
def output_json(data, code, headers=None):
    resp = make_response(json.dumps(data), code)
    resp.headers.extend(headers or {})
    return resp

class Shipments_by_status(Resource):
    @marshal_with(shipments_fields)
    def get(self, status):
        return models.Shipment.query.filter_by(status=status).all()