Created
June 15, 2020 10:34
-
-
Save raghavmri/3af0bf9903bb4ef8508546688f3d6a06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using flask_restful | |
from flask import Flask, jsonify, request | |
from flask_restful import Resource, Api | |
# creating the flask app | |
app = Flask(__name__) | |
# creating an API object | |
api = Api(app) | |
# making a class for a particular resource | |
# the get, post methods correspond to get and post requests | |
# they are automatically mapped by flask_restful. | |
# other methods include put, delete, etc. | |
class Hello_World(Resource): | |
# corresponds to the GET request. | |
# this function is called whenever there | |
# is a GET request for this resource | |
def get(self): | |
return jsonify({'message': 'hello world'}) | |
# Corresponds to POST request | |
def post(self): | |
data = request.get_json() # status code | |
return jsonify({'data': data}), 201 | |
# adding the defined resources along with their corresponding urls | |
api.add_resource(Hello_World, '/') | |
# driver function | |
if __name__ == '__main__': | |
app.run(debug = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment