Skip to content

Instantly share code, notes, and snippets.

@brizandrew
Last active December 19, 2023 10:16
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save brizandrew/e5ff878f8e3c17386de037c201386dc1 to your computer and use it in GitHub Desktop.
Save brizandrew/e5ff878f8e3c17386de037c201386dc1 to your computer and use it in GitHub Desktop.
Simple Flask Webhook Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, request
from urllib import unquote_plus
import json
import re
app = Flask(__name__)
def parse_request(req):
"""
Parses application/json request body data into a Python dictionary
"""
payload = req.get_data()
payload = unquote_plus(payload)
payload = re.sub('payload=', '', payload)
payload = json.loads(payload)
return payload
@app.route('/', methods=['GET'])
def index():
"""
Go to localhost:5000 to see a message
"""
return ('This is a website.', 200, None)
@app.route('/api/print', methods=['POST'])
def print_test():
"""
Send a POST request to localhost:5000/api/print with a JSON body with a "p" key
to print that message in the server console.
"""
payload = parse_request(request)
print payload['p']
return ("", 200, None)
@app.route('/api/sum', methods=['POST'])
def sum():
"""
Send a POST request to localhost:5000/api/sum with a JSON body with an "a" and "b" key
to have the app add those numbers together and return a response string with their sum.
"""
print "Processing request..."
payload = parse_request(request)
print "Receieved following paylod:"
print payload
print "Adding sum..."
summation = payload['a'] + payload['b']
print "Found sum: %s" % summation
print "Creating response string..."
resp = '%s + %s = %s' % (payload['a'], payload['b'], summation)
print "Sending the following response:"
print resp
return (resp, 200, None)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)
@eshaan7
Copy link

eshaan7 commented Jul 1, 2020

Nice tutorial. Also, for creating webhooks from shell commands checkout my flask extension: Flask-shell2http. (shameless plug)

@hal98x
Copy link

hal98x commented Feb 14, 2021

@brizandrew
Thanks for posting this example it worked well. I have a cautionary note for python 3

For Python 3 the parse_request function can be replaced with
payload=request.get_json()

and of course replace print statements with print()

@Impulsleistung
Copy link

Calling http://127.0.0.1:5000/api/print?p=singleString with POST
gives me in POSTMAN an error:

<title>500 Internal Server Error</title>

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

@dranastos
Copy link

how do you change the port and network adapter that it listens on?

@a14stoner
Copy link

how do you change the port and network adapter that it listens on?

Hi @dranastos:

this should be possible with:

if __name__ == '__main__':
    app.run(host=xxxxx,port=xxxx,debug=True, use_reloader=True)
 

@calebshannon
Copy link

calebshannon commented May 14, 2022

@brizandrew Thanks for posting this example it worked well. I have a cautionary note for python 3

For Python 3 the parse_request function can be replaced with payload=request.get_json()

and of course replace print statements with print()

Thanks, I am just starting with Flask on Python 3.x and this helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment