mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 23:00:18 +00:00
use flask's jsonify
This commit is contained in:
+17
-24
@@ -8,7 +8,7 @@ This module provides the core HttpBin experience.
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
from flask import Flask, request, render_template, redirect
|
||||
from flask import Flask, request, render_template, redirect, jsonify
|
||||
from werkzeug.datastructures import WWWAuthenticate
|
||||
|
||||
|
||||
@@ -46,67 +46,63 @@ def view_landing_page():
|
||||
|
||||
|
||||
@app.route('/ip')
|
||||
@filters.json
|
||||
def view_origin():
|
||||
"""Returns Origin IP."""
|
||||
|
||||
return dict(origin=request.remote_addr)
|
||||
return jsonify(origin=request.remote_addr)
|
||||
|
||||
|
||||
@app.route('/headers')
|
||||
@filters.json
|
||||
def view_headers():
|
||||
"""Returns HTTP HEADERS."""
|
||||
|
||||
return get_dict('headers')
|
||||
return jsonify(get_dict('headers'))
|
||||
|
||||
|
||||
@app.route('/user-agent')
|
||||
@filters.json
|
||||
def view_user_agent():
|
||||
"""Returns User-Agent."""
|
||||
|
||||
headers = get_headers()
|
||||
|
||||
return {'user-agent': headers['user-agent']}
|
||||
return jsonify({'user-agent': headers['user-agent']})
|
||||
|
||||
|
||||
@app.route('/get', methods=('GET',))
|
||||
@filters.json
|
||||
def view_get():
|
||||
"""Returns GET Data."""
|
||||
|
||||
return get_dict('url', 'args', 'headers', 'origin')
|
||||
return jsonify(get_dict('url', 'args', 'headers', 'origin'))
|
||||
|
||||
|
||||
|
||||
@app.route('/post', methods=('POST',))
|
||||
@filters.json
|
||||
|
||||
def view_post():
|
||||
"""Returns POST Data."""
|
||||
|
||||
return get_dict('url', 'args', 'form', 'data', 'origin', 'headers', 'files')
|
||||
return jsonify(get_dict(
|
||||
'url', 'args', 'form', 'data', 'origin', 'headers', 'files'))
|
||||
|
||||
|
||||
@app.route('/put', methods=('PUT',))
|
||||
@filters.json
|
||||
def view_put():
|
||||
"""Returns PUT Data."""
|
||||
|
||||
return get_dict('url', 'args', 'form', 'data', 'origin', 'headers', 'files')
|
||||
return jsonify(get_dict(
|
||||
'url', 'args', 'form', 'data', 'origin', 'headers', 'files'))
|
||||
|
||||
|
||||
@app.route('/patch', methods=('PATCH',))
|
||||
@filters.json
|
||||
def view_patch():
|
||||
"""Returns PATCH Data."""
|
||||
|
||||
return get_dict('url', 'args', 'form', 'data', 'origin', 'headers', 'files')
|
||||
return jsonify(get_dict(
|
||||
'url', 'args', 'form', 'data', 'origin', 'headers', 'files'))
|
||||
|
||||
|
||||
|
||||
@app.route('/delete', methods=('DELETE',))
|
||||
@filters.json
|
||||
def view_delete():
|
||||
"""Returns DETLETE Data."""
|
||||
|
||||
@@ -115,11 +111,11 @@ def view_delete():
|
||||
|
||||
@app.route('/gzip')
|
||||
@filters.gzip
|
||||
@filters.json
|
||||
def view_gzip_encoded_content():
|
||||
"""Returns GZip-Encoded Data."""
|
||||
|
||||
return get_dict('origin', 'headers', method=request.method, gzipped=True)
|
||||
return jsonify(get_dict(
|
||||
'origin', 'headers', method=request.method, gzipped=True))
|
||||
|
||||
|
||||
@app.route('/redirect/<int:n>')
|
||||
@@ -159,7 +155,6 @@ def view_status_code(code):
|
||||
|
||||
|
||||
@app.route('/cookies')
|
||||
@filters.json
|
||||
def view_cookies(hide_env=True):
|
||||
"""Returns cookie data."""
|
||||
|
||||
@@ -172,7 +167,7 @@ def view_cookies(hide_env=True):
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
return dict(cookies=cookies)
|
||||
return jsonify(cookies=cookies)
|
||||
|
||||
|
||||
@app.route('/cookies/set/<name>/<value>')
|
||||
@@ -186,17 +181,16 @@ def set_cookie(name, value):
|
||||
|
||||
|
||||
@app.route('/basic-auth/<user>/<passwd>')
|
||||
@filters.json
|
||||
def basic_auth(user='user', passwd='passwd'):
|
||||
"""Prompts the user for authorization using HTTP Basic Auth."""
|
||||
|
||||
if not check_basic_auth(user, passwd):
|
||||
return status_code(401)
|
||||
return dict(authenticated=True, user=user)
|
||||
|
||||
return jsonify(authenticated=True, user=user)
|
||||
|
||||
|
||||
@app.route('/hidden-basic-auth/<user>/<passwd>')
|
||||
@filters.json
|
||||
def hidden_basic_auth(user='user', passwd='passwd'):
|
||||
"""Prompts the user for authorization using HTTP Basic Auth."""
|
||||
|
||||
@@ -206,7 +200,6 @@ def hidden_basic_auth(user='user', passwd='passwd'):
|
||||
|
||||
|
||||
@app.route('/digest-auth/<qop>/<user>/<passwd>')
|
||||
@filters.json
|
||||
def digest_auth(qop=None, user='user', passwd='passwd'):
|
||||
"""Prompts the user for authorization using HTTP Digest auth"""
|
||||
if qop not in ('auth', 'auth-int'):
|
||||
|
||||
Reference in New Issue
Block a user