当前位置: 首页>>代码示例>>Python>>正文


Python response.json方法代码示例

本文整理汇总了Python中sanic.response.json方法的典型用法代码示例。如果您正苦于以下问题:Python response.json方法的具体用法?Python response.json怎么用?Python response.json使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sanic.response的用法示例。


在下文中一共展示了response.json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_uri_with_different_method_and_different_params

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_uri_with_different_method_and_different_params(app):
    @app.route("/ads/<ad_id>", methods=["GET"])
    async def ad_get(request, ad_id):
        return json({"ad_id": ad_id})

    @app.route("/ads/<action>", methods=["POST"])
    async def ad_post(request, action):
        return json({"action": action})

    request, response = app.test_client.get("/ads/1234")
    assert response.status == 200
    assert response.json == {"ad_id": "1234"}

    request, response = app.test_client.post("/ads/post")
    assert response.status == 200
    assert response.json == {"action": "post"} 
开发者ID:huge-success,项目名称:sanic,代码行数:18,代码来源:test_routes.py

示例2: test_json_content_type

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_json_content_type(app):
    @app.get("/json")
    def send_json(request):
        return json({"foo": "bar"})

    @app.get("/text")
    def send_text(request):
        return text("foobar")

    @app.get("/custom")
    def send_custom(request):
        return text("foobar", content_type="somethingelse")

    _, response = await app.asgi_client.get("/json")
    assert response.headers.get("content-type") == "application/json"

    _, response = await app.asgi_client.get("/text")
    assert response.headers.get("content-type") == "text/plain; charset=utf-8"

    _, response = await app.asgi_client.get("/custom")
    assert response.headers.get("content-type") == "somethingelse" 
开发者ID:huge-success,项目名称:sanic,代码行数:23,代码来源:test_asgi.py

示例3: test_request_multipart_file_with_json_content_type

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_request_multipart_file_with_json_content_type(app):
    @app.route("/", methods=["POST"])
    async def post(request):
        return text("OK")

    payload = (
        "------sanic\r\n"
        'Content-Disposition: form-data; name="file"; filename="test.json"\r\n'
        "Content-Type: application/json\r\n"
        "Content-Length: 0"
        "\r\n"
        "\r\n"
        "------sanic--"
    )

    headers = {"content-type": "multipart/form-data; boundary=------sanic"}

    request, _ = app.test_client.post(data=payload, headers=headers)
    assert request.files.get("file").type == "application/json" 
开发者ID:huge-success,项目名称:sanic,代码行数:21,代码来源:test_requests.py

示例4: test_request_multipart_file_without_field_name

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_request_multipart_file_without_field_name(app, caplog):
    @app.route("/", methods=["POST"])
    async def post(request):
        return text("OK")

    payload = (
        '------sanic\r\nContent-Disposition: form-data; filename="test.json"'
        "\r\nContent-Type: application/json\r\n\r\n\r\n------sanic--"
    )

    headers = {"content-type": "multipart/form-data; boundary=------sanic"}

    request, _ = app.test_client.post(
        data=payload, headers=headers, debug=True
    )
    with caplog.at_level(logging.DEBUG):
        request.form

    assert caplog.record_tuples[-1] == (
        "sanic.root",
        logging.DEBUG,
        "Form-data field does not have a 'name' parameter "
        "in the Content-Disposition header",
    ) 
开发者ID:huge-success,项目名称:sanic,代码行数:26,代码来源:test_requests.py

示例5: test_request_multipart_with_multiple_files_and_type

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_request_multipart_with_multiple_files_and_type(app):
    @app.route("/", methods=["POST"])
    async def post(request):
        return text("OK")

    payload = (
        '------sanic\r\nContent-Disposition: form-data; name="file"; filename="test.json"'
        "\r\nContent-Type: application/json\r\n\r\n\r\n"
        '------sanic\r\nContent-Disposition: form-data; name="file"; filename="some_file.pdf"\r\n'
        "Content-Type: application/pdf\r\n\r\n\r\n------sanic--"
    )
    headers = {"content-type": "multipart/form-data; boundary=------sanic"}

    request, _ = app.test_client.post(data=payload, headers=headers)
    assert len(request.files.getlist("file")) == 2
    assert request.files.getlist("file")[0].type == "application/json"
    assert request.files.getlist("file")[1].type == "application/pdf" 
开发者ID:huge-success,项目名称:sanic,代码行数:19,代码来源:test_requests.py

示例6: test_request_multipart_with_multiple_files_and_type_asgi

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_request_multipart_with_multiple_files_and_type_asgi(app):
    @app.route("/", methods=["POST"])
    async def post(request):
        return text("OK")

    payload = (
        '------sanic\r\nContent-Disposition: form-data; name="file"; filename="test.json"'
        "\r\nContent-Type: application/json\r\n\r\n\r\n"
        '------sanic\r\nContent-Disposition: form-data; name="file"; filename="some_file.pdf"\r\n'
        "Content-Type: application/pdf\r\n\r\n\r\n------sanic--"
    )
    headers = {"content-type": "multipart/form-data; boundary=------sanic"}

    request, _ = await app.asgi_client.post("/", data=payload, headers=headers)
    assert len(request.files.getlist("file")) == 2
    assert request.files.getlist("file")[0].type == "application/json"
    assert request.files.getlist("file")[1].type == "application/pdf" 
开发者ID:huge-success,项目名称:sanic,代码行数:19,代码来源:test_requests.py

示例7: test_bp_strict_slash

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_bp_strict_slash(app):
    bp = Blueprint("test_text")

    @bp.get("/get", strict_slashes=True)
    def get_handler(request):
        return text("OK")

    @bp.post("/post/", strict_slashes=True)
    def post_handler(request):
        return text("OK")

    app.blueprint(bp)

    request, response = app.test_client.get("/get")
    assert response.text == "OK"
    assert response.json is None

    request, response = app.test_client.get("/get/")
    assert response.status == 404

    request, response = app.test_client.post("/post/")
    assert response.text == "OK"

    request, response = app.test_client.post("/post")
    assert response.status == 404 
开发者ID:huge-success,项目名称:sanic,代码行数:27,代码来源:test_blueprints.py

示例8: test_route_handler_add

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test_route_handler_add(app: Sanic):
    view = CompositionView()

    async def get_handler(request):
        return json({"response": "OK"})

    view.add(["GET"], get_handler, stream=False)

    async def default_handler(request):
        return text("OK")

    bp = Blueprint(name="handler", url_prefix="/handler")
    bp.add_route(default_handler, uri="/default/", strict_slashes=True)

    bp.add_route(view, uri="/view", name="test")

    app.blueprint(bp)

    _, response = app.test_client.get("/handler/default/")
    assert response.text == "OK"

    _, response = app.test_client.get("/handler/view")
    assert response.json["response"] == "OK" 
开发者ID:huge-success,项目名称:sanic,代码行数:25,代码来源:test_blueprints.py

示例9: index

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def index(request):
    token = request.headers.get("X-Token")
    if token not in G.config.tokens:
        C["global"].update({"401": 1})
        raise SanicException("Unauthorized", status_code=401)
    try:
        latitude = float(request.args.get("lat", default=None))
        longitude = float(request.args.get("lon", default=None))
        ghash = geohash.encode(latitude, longitude, G.config.precision)
    except:
        C["stats"][token[:6]].update({"400": 1})
        raise SanicException("Bad Request", status_code=400)
    try:
        data = search(ghash)
        if data is None:
            C["stats"][token[:6]].update({"404": 1})
            return jsonify(G.config.fallback)
        else:
            C["stats"][token[:6]].update({"200": 1})
            return jsonify(data)
    except:
        C["stats"][token[:6]].update({"500": 1})
        return jsonify(G.config.fallback) 
开发者ID:uol,项目名称:geo-br,代码行数:25,代码来源:sapp.py

示例10: test

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test(request):
    return response.json({"answer": "42"}) 
开发者ID:huge-success,项目名称:sanic,代码行数:4,代码来源:run_async_advanced.py

示例11: foo

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def foo(request):
    return json({'msg': 'hi from blueprint'}) 
开发者ID:huge-success,项目名称:sanic,代码行数:4,代码来源:blueprints.py

示例12: foo2

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def foo2(request):
    return json({'msg': 'hi from blueprint2'}) 
开发者ID:huge-success,项目名称:sanic,代码行数:4,代码来源:blueprints.py

示例13: handler_json

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def handler_json(request):
    return response.json({"foo": "bar"}) 
开发者ID:huge-success,项目名称:sanic,代码行数:4,代码来源:run_asgi.py

示例14: test

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test(request):
    log.debug('X-Request-ID: %s', context.get('X-Request-ID'))
    log.info('Hello from test!')
    return response.json({"test": True}) 
开发者ID:huge-success,项目名称:sanic,代码行数:6,代码来源:log_request_id.py

示例15: test

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import json [as 别名]
def test(request):
    return json({'status': 'authorized'}) 
开发者ID:huge-success,项目名称:sanic,代码行数:4,代码来源:authorized_sanic.py


注:本文中的sanic.response.json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。