Test Your Django Web App with Ngrok

Oct. 16, 2020, 12:51 p.m.
Django · 3 min read
Test Your Django Web App with Ngrok

What is Ngrok?

Ngrok is an application that exposes a local development server to the internet.  In other words, the program allows an application on your local device to become publicly accessible. Ngrok is primarily used to test and debug webhooks.  However, it's also useful for sharing an early MVP from your local device with colleagues and friends before deployment.

So how does it work?  After downloading and running Ngork, specify the port of a web server.  Then Ngork works by connecting your local web server to the Ngork cloud service, which relays access to a public URL.  Some features include secure tunnels with an HTTPS URL, HTTP auth credentials to control access, and automation with the Ngork API. 


Using Ngork with the Django Rest Framework

Ngork is a handy tool when building with the Django Rest Framework(DRF).  DRF is a set of tools for building web applications with a variety of endpoints, known as APIs.  For example, large platforms such as Twitter and Facebook have APIs for programmers to request information and automate processes such as posting and liking by making requests to different endpoints.  When creating your own API, it's important to test such endpoints on your local development server before deployment.  Ngrok enables you to do so.


DRF and Ngork Quick Tutorial:

  • Download the official release here: https://ngrok.com/downloadngrok download page preview
  • Unzip after downloading then click/run "ngrok.exe" to open the ngrok terminal
  • Terminal preview:running ngrok exe 
  • Using the ngrok terminal, you can select the port to connect to the ngrok cloud service for public access
  • Imagine a django project is already running at port 8000 in another terminal
  • Enter the following line in the ngrok terminal. Make sure to run the command in the same directory containing the ngrok executable:
    C:\Users\Owner\Desktop\code>ngrok http 8000​
  • You should see your port forwarding to a custom ngrok address.  Of course with premium features you can customize your address.  For now ours looks like the following:ngrok port forwarding django
  • As you can see, I can now access my web app from either an http or https web address.  If I open my browser to I also have a debugging page that shows requests and any parameters being sent.  This page in particular is super helpful when testing a DRF project.  
  • For example, I'll make a post request to my DRF application using python requests:
    >>> import requests
    >>> data = {
    ... 'SRC': 'SNR7',
    ... 'Landing_Page': 'www.test.com',
    ... 'IP_Address': '111111111',
    ... 'First_Name': 'test',
    ... 'Last_Name': 'test1',
    ... 'Address': '111 test',
    ... 'City': 'San Diego',
    ... 'State': 'CA',
    ... 'Zip_Code': '92109',
    ... 'Phone': '7607777777',
    ... 'Universal_LeadiD': 'jleadid1',
    ... 'Email': 'tester@gmail.com',
    ... 'Date_Of_Lead':'2020-09-15',
    ... 'Credit_Rating': 'good',
    ... 'Homeowner': 'yes',
    ... 'Date_Of_Birth': '1970-09-22',
    ... }
    >>> url = "https://3c597d411842.ngrok.io/api"
    >>> requests.post(url, data=data)
    <Response [400]>​
  • And the debugging page:ngrok debug panel


Hopefully this helps you understand how to test your Django project on the web without deploying.  Ngrok is easy and fast to setup and can definitely save time debugging.  Personally, I use Ngrok often when I make changes to my API, test webhooks, or need to show a client how a project is shaping up.  

Post a Comment
Join the community

Written By
I enjoy writing about saas, founders, and Django. Let me know if I should write about you!