本文整理匯總了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"}
示例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"
示例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"
示例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",
)
示例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"
示例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"
示例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
示例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"
示例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)
示例10: test
# 需要導入模塊: from sanic import response [as 別名]
# 或者: from sanic.response import json [as 別名]
def test(request):
return response.json({"answer": "42"})
示例11: foo
# 需要導入模塊: from sanic import response [as 別名]
# 或者: from sanic.response import json [as 別名]
def foo(request):
return json({'msg': 'hi from blueprint'})
示例12: foo2
# 需要導入模塊: from sanic import response [as 別名]
# 或者: from sanic.response import json [as 別名]
def foo2(request):
return json({'msg': 'hi from blueprint2'})
示例13: handler_json
# 需要導入模塊: from sanic import response [as 別名]
# 或者: from sanic.response import json [as 別名]
def handler_json(request):
return response.json({"foo": "bar"})
示例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})
示例15: test
# 需要導入模塊: from sanic import response [as 別名]
# 或者: from sanic.response import json [as 別名]
def test(request):
return json({'status': 'authorized'})