Skip to content

Instantly share code, notes, and snippets.

@hxer
Created March 2, 2016 04:45
Show Gist options
  • Save hxer/743e5cb1a0f75455a5e4 to your computer and use it in GitHub Desktop.
Save hxer/743e5cb1a0f75455a5e4 to your computer and use it in GitHub Desktop.
Receiving and POSTing JSON with requests [Python and Flask]
import os
# Using Flask since Python doesn't have built-in session management
from flask import Flask, session, render_template
# Our target library
import requests
import json
app = Flask(__name__)
# Generate a secret random key for the session
app.secret_key = os.urandom(24)
# Define routes for the examples to actually run
@app.route('/run_get')
def run_get():
url = 'https://api.github.com/users/runnable'
# this issues a GET to the url. replace "get" with "post", "head",
# "put", "patch"... to make a request using a different method
r = requests.get(url)
return json.dumps(r.json(), indent=4)
@app.route('/run_post')
def run_post():
url = 'http://httpbin.org/post'
data = {'a': 10, 'b': [{'c': True, 'd': False}, None]}
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=json.dumps(data), headers=headers)
return json.dumps(r.json(), indent=4)
# Define a route for the webserver
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment