Adding Support to Handle a POST request

None

Tip

Adding Support to Handle a POST request


In our index.html file, we have configured the form request to submit as a POST request. Therefore, we’ll need to handle the GET request to load the initial form and then the POST request after the user has filled out the form and submitted it.


For more on GET vs POST Requests, read this.


Getting POST Request Data


After you’ve filled out the form on http://localhost:8000 and click the submit button. You’ll need to get the data that the user entered and make an API request to ZBD to process the payment.


In the views.py file, update our index function’s code.


...

def index(request):
		# handles the post request
    if request.POST:
        return render(request, 'lightning_address_payment/index.html')
		# handles the get request
    else:
        return render(request, 'lightning_address_payment/index.html')

...


Now the GET request all appears fine. Let’s get the data from the user submitting the form.


Reviewing the HTML file, we named the Lightning Address field lnaddress and the amount field amount .



Django makes it really easy to parse form data, so let’s go ahead and do that.



All you need to do is use the request.POST object to see which data is incoming on the request. We can ignore the csrfmiddlewaretoken for the scope of this tutorial. We can access the lnaddress and amount properties to pass them to the ZBD API. Let’s save them as variables.


...

def index(request):
    if request.POST:
				amount = request.POST["amount"] + "000"
        # Form Data
				data = { 'lnAddress' : request.POST["lnaddress"], 'amount' : amount}

        return render(request, 'lightning_address_payment/index.html')
    else:
        return render(request, 'lightning_address_payment/index.html')

...


Great! Let’s now take a look at the ZBD API. Here’s the Send Payment to Lightning Address API documentation.


Plugging in the ZBD API


First, we need to install a new library. Install the python requests library. Press CMD (Ctrl) + C to turn off your webserver, if needed. Then execute the following command in your terminal.


pip3 install requests



Import the requests library now at the top of the file that you have it installed.


from django.shortcuts import render
from django.http import HttpResponse
import requests

# Create your views here.

def index(request):
    if request.POST:
				# Form Data
				# We add three zeroes as a string at the end to convert to msats.
        amount = request.POST["amount"] + "000"
				data = { 'lnAddress' : request.POST["lnaddress"], 'amount' : amount, 'comment': 'Sending to a Lightning Address'}
        return render(request, 'lightning_address_payment/index.html')
    else:
        return render(request, 'lightning_address_payment/index.html')

...


Grab your apikey from your project in the Developer Dashboard project. Make sure you have transferred sats to this project!

  • If you haven’t already, Top Up with MoonPay or create a charge to deposit funds into your Developer Wallet and then Transfer the funds to to your project!



Now let’s add the properties needed for the request.posts function that we imported. We’ll add the properties and form the request. Please review the code below and make adjustments, or just copy and paste.


...

def index(request):
    if request.POST:
        # Form Data
        amount = request.POST["amount"] + "000"

        # ZBD API 
        URL = "<https://api.zebedee.io/v0/ln-address/send-payment>"
        data = { 'lnAddress' : request.POST["lnaddress"], 'amount' : amount, 'comment': 'Sending to a Lightning Address'}
				
				# ADD YOUR API KEY BELOW!
        APIKEY = 'YOUR_API_KEY_HERE'
				############################
        headers = { "Content-Type":"application/json",  'apikey' : APIKEY }

        # sending get request and saving the response as response object
        r = requests.post(url = URL, json = data , headers = headers )
        # extracting data in json format
        data = r.json()

        print(data)

        if data["success"]:
            return redirect('success')
        else:
            return render(request, 'lightning_address_payment/index.html')

    else:
        return render(request, 'lightning_address_payment/index.html')

...


Whew. Now we’re all ready to go! Here’s the complete views.py file.


from django.shortcuts import render, redirect
import requests, os

# Create your views here.

def index(request):
    if request.POST:
        # Form Data
        amount = request.POST["amount"] + "000"

        # ZBD API 
        URL = "<https://api.zebedee.io/v0/ln-address/send-payment>"
        data = { 'lnAddress' : request.POST["lnaddress"], 'amount' : amount, 'comment': 'Sending to a Lightning Address'}
				# ADD YOUR API KEY BELOW!
        APIKEY = 'YOUR_API_KEY_HERE'
				############################
        headers = { "Content-Type":"application/json",  'apikey' : APIKEY }

        # sending get request and saving the response as response object
        r = requests.post(url = URL, json = data , headers = headers )
        # extracting data in json format
        data = r.json()

        print(data)

        if data["success"]:
            return redirect('success')
        else:
            return render(request, 'lightning_address_payment/index.html')

    else:
        return render(request, 'lightning_address_payment/index.html')

def success(request):
    return render(request, 'lightning_address_payment/success.html')