
Brandon T. answered 05/25/19
Python Expert
Hi Caitlin,
My suggestion to you would be to use a Python Framework to make your life a lot easier when developing a REST API. I personally like to use Flask for REST API development. If you are looking for a Python Framework that gives you more security out of the box I would instead suggest Django.
Here is an example of a simple POST/GET endpoint with Flask:
@app.route('/new-user', methods=['POST'])
def new_user():
#Do User stuff
@app.route('/user', methods=['GET'])
def user():
#Do User stuff
@app.route('/user/data', methods=['POST', 'GET'])
def user_data():
#Do CRUD User Data stuff
As you can see above, with flask we use a decorator to define a route for the endpoint url with parameter for methods allowed. If you tried to POST to a GET endpoint, you would get an error. The route will use the function under the decorator each time that endpoint is hit. It's here that you would either serve a user-facing view or return data back. This includes sending server status codes like a 404 or 500.
I need to mention that it is advised that with any production REST API you use middleware, encryption and authentication to ensure the security of your API. But this is a great way to setup a basic REST style API to practice with.
With flask, you can read header data, query parameter data, etc... Basically everything you'd expect to get out of any other backend framework like NodeJS or Laravel.
I think this should be enough to help get you going to start learning the basics and I would be happy to answer any further questions you may have about flask for web API's.
Best,
Brandon T.