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’
Error

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.

Asked By: Charlie

||

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.

Answered By: John Hanley

You can use XMLHttpRequest module

Answered By: user21349535
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.