Call apis on web with py-script
Question:
I try to call an API with http.client th error says that the ‘http.client’ has no attribute ‘HTTPSConnection’
The code is:
import http.client
conn = http.client.HTTPSConnection("www.banxico.org.mx")
payload = ''
headers = {}
conn.request("GET", "/SieAPIRest/service/v1/series/SP68257/datos/2022-11-11/2022-11-11?token=04021aac739b77e232d9670147936836e9e9fc31e08bde26665c0f013df94471", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
The code work well on jupyther notebook, but not in web.
Answers:
The package http.client
depends on modules such as ssl
which are not supported when running inside the web browser. Rewrite your code to use supported APIs such as pyfetch
.
You can use XMLHttpRequest module
I try to call an API with http.client th error says that the ‘http.client’ has no attribute ‘HTTPSConnection’
The code is:
import http.client
conn = http.client.HTTPSConnection("www.banxico.org.mx")
payload = ''
headers = {}
conn.request("GET", "/SieAPIRest/service/v1/series/SP68257/datos/2022-11-11/2022-11-11?token=04021aac739b77e232d9670147936836e9e9fc31e08bde26665c0f013df94471", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
The code work well on jupyther notebook, but not in web.
The package http.client
depends on modules such as ssl
which are not supported when running inside the web browser. Rewrite your code to use supported APIs such as pyfetch
.
You can use XMLHttpRequest module