Test the DRF with Swagger UI

In the last post, we discussed the Django REST Framework, or DRF for short. The DRF lets you create really simple and powerful APIs with very little coding. Using Models and Model Serializers with generic List and Update Views, you can have a working API in no time at all.

The source code for the project DRF by Example is located on GitHub.

Now that we have a solid foundation for our project and a RESTful API design pattern, we can begin documenting our API as well as provide a cool testing environment with Swagger UI.

To enable Swagger UI in your API project, for Django and DRF...

(.env) $ pip3 install django-swagger

Add this to your root URLs conf.

From the Gist above, we set schema_view to our Swagger definition.

schema_view = get_swagger_view(title='DRF by Example')

Then wire up the route: /api/docs/ to schema_view.

Next, in your browser, go to: http://localhost:8888/api/docs/

Now, start testing your API...

In my example from the last post, we are going to GeoLocate an IP Address. Select locations and expand the list. Select geolocate and click Try It Out. Enter your IP address and click Execute.


You can see in the image above, the nicely formatted response body, reponse headers, response status code.

Wrapping Up

The DRF makes it ridiculously simple to create killer APIs with little effort using a set of very specific design patterns.

Swagger UI extends the API development toolset by helping you test and document your API while you are building. When testing, use Swagger to check the response body data and response codes the server sends back to the test client. This really helps speed up development and ensures the API is responding correctly.

Learn more about Swagger UI.

Craig Derington

Secular Humanist, Libertarian, FOSS Evangelist building Cloud Apps developed on Red Hat Enterprise Linux and Ubuntu Server. My toolset includes Python, Celery, Flask, Django, MySQL, MongoDB and Git.

comments powered by Disqus