Anvil HTTP Get Method "Headers Not Defined

What I’m trying to do:
Hi,

I am trying to get the results of my API when the button is clicked. But each time I try the terminal shows an error " NameError: name 'headers' is not defined". I’ve tried testing my code on visual studio and it works perfectly but not for anvil.

Code Sample:

from anvil import *
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.http
import json

class Form1(Form1Template):  #anvil.http.request

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.

  def button_1_click(self, **event_args):
    url = "https://breachdirectory.p.rapidapi.com/"

    querystring = {"func":"auto","term":self.UserData}
    response = anvil.http.request(url, method="GET", headers=headers, params=querystring)
    data = str(json.loads(response.text)) #12 char position 
    p = data[12]
    if p == 'T':
      self.output = "Your Data Has Been Leaked!"
    else: 
      self.output = "Your Data Has Not Been Leaked"
    
    
    """This method is called when the button is clicked"""
    pass

    

                   

Clone link:
Anvil | Login

I’m still learning Python, but with the error it seems like your issue is here:

response = anvil.http.request(url, method="GET", headers=headers, params=querystring)

I don’t see a headers variable in your code so you are defining a header in the request with no data. If a header is not needed then removed headers=headers or use a header and see if that works.

4 Likes