Error message : Http not defined / API import Python

Viewed 1034

I'm trying to import the URL of the Json API but I get the error message:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-5f40dac6d9a4> in <module>
      1 # get data from the API
      2 url = 'https://admin.appnext.com/offerApi.aspx?pimg=1&city=1&ua=1&id=5da11d20-0a32-4852-8233-39b711a360a9'
----> 3 r = http.request('GET', url)
      4 r.status

NameError: name 'http' is not defined

The code I'm using is the follow:

#  data retrieval
import urllib3
from urllib3 import request
# json data
import json
# pandas dataframes
import pandas as pd

No issues to import

# get data from the API
url = 'https://admin.appnext.com/offerApi.aspx?pimg=1&city=1&ua=1&id=5da11d20-0a32-4852-8233-39b711a360a9'
r = http.request('GET', url)
r.status

Does anybody know what could be the issue?

Thanks in advance

3 Answers

You didn't define the variable http anywhere in the code you showed. You first need to instantiate a new request from the connection pool manager and then you can perform the request:

#  data retrieval
import urllib3
from urllib3 import request
# json data
import json
# pandas dataframes
import pandas as pd

# get data from the API
http = urllib3.PoolManager()
url = 'https://admin.appnext.com/offerApi.aspx?pimg=1&city=1&ua=1&id=5da11d20-0a32-4852-8233-39b711a360a9'
r = http.request('GET', url)
r.status

For reference: https://urllib3.readthedocs.io/en/latest/

HTTP is not defined in your code, so you can't use that. I would advice you to checkout W3Schools guide on the "requests" module in python, and check out their examples on how to perform requests: https://www.w3schools.com/python/module_requests.asp.

You could then perform the request like this:

#  data retrieval
import requests
# json data
import json
# pandas dataframes
import pandas as pd

url = 'https://admin.appnext.com/offerApi.aspx?pimg=1&city=1&ua=1&id=5da11d20-0a32-4852-8233-39b711a360a9'
r = requests.get(url)
print(r.status)

you need to use urllib.request.urlopen()

the code will be like this:

import urllib.request

url = 'https://admin.appnext.com/offerApi.aspx?pimg=1&city=1&ua=1&id=5da11d20-0a32-4852-8233-39b711a360a9'
x = urllib.request.urlopen(url)
print(x.read())
Related