1. Background
Ok, maybe a lot of friends like me often use a variety of APIs, such as Facebook, GitHub, and even API. So many people also want to make their own API. Online on this aspect of the course is very few, today I have done one, the method published.
First show the effect:
Using the "Curl" method, return a JSON, you can also try:
Curl-i HTTP://OSPAFZONE.DUAPP.COM/OSPAF
2. Code
First of all, the environment, is python+flask, especially good configuration.
From flask import flask, Jsonifyapp = Flask (__name__) tasks = [ { ' id ': 1, ' title ': U ' OSPA ', ' description ': U ' this was Ospaf-api test ', ' done ': False }, { ' id ': 2, ' title ': U ' Garvin ', ' description ': U ' I am Garvin ', ' done ': False }] @app. Route ('/', methods=[' GET ') def home (): return jsonify ({' Tasks ': Tasks}) If __name__ = = ' __main__ ': app.run (debug=true)
This is the local running environment of the code, after the execution can be entered in the browser "Http://127.0.0.1:5000/", you can see the effect, the return is a JSON-formatted file.
Attention:
@app. Route ('/', methods=[' GET ')
This line of code '/' corresponds to the following function definition Def home (), debug url "http://127.0.0.1:5000/"
If it is '/OSPAF ', the corresponding function defines Def OSPAF (), the debug URL "HTTP://127.0.0.1:5000/OSPAF"
OK, it should be said very clearly.
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
Make your own API with Python+flask (tutorial with source code)