anvil.http.request stoped working.

Hi,

I am using this function to request ticket prices in Binance (crypto exchange):

def get_price(code):
  response = anvil.http.request(f"https://www.binance.com/api/v3/ticker/price?symbol={code}USDT", json=True)
  return float(response['price'])

This function has worked well till today. Now in the app I receive this error:

ValueError: Returned data is not valid JSON

I tried to implement a version of the function in the server side using requests and I receive this error:

JSONDecodeError: [Errno Expecting value] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: xvjtZOOi2UI7yY--vGAay2z_vQs4TxR-i-Ioz9R8wo1DUn0ilro-Mg==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>: 0
at /usr/local/lib/python3.7/site-packages/requests/models.py, line 917

Are these problems temporal?

I can access to the URL with no problems from my browser: https://www.binance.com/api/v3/ticker/price?symbol=DOGEUSDT

Any help will be appreciated!

Matias

That’s an error from the CDN itself, not anything related to Anvil. CloudFront does a variety of checks to make sure that requests are valid (not sure what all they do), and apparently the programmatic request is failing one of those checks. The fact that you had the trouble from server code, too, suggests that it isn’t CORS based.

Have you tried the request from a standalone Python program on your computer? That’d at least tell you something about the conditions under which requests are refused.

Hi! thanks for answering… Locally the requests to the API work perfectly:

Python 3.8.10 (default, Jun 22 2022, 20:18:18) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get("https://www.binance.com/api/v3/ticker/price?symbol=BTCUSDT")
>>> r.json()
{'symbol': 'BTCUSDT', 'price': '19588.43000000'}

But as I said, using requests.get in the server side also fails. That’s weird… :frowning:

Yeah, that’s strange. Without more information from CloudFront, though, I’m not sure how you’d work out what about the Anvil client and server requests is causing them to be blocked. I suspect you’d need to start with Binance support, there should be logs they can check given the request id.