Get call failing on some APIs

Hi,

I am trying to use a few example APIs in prep for building a restful API suite someday. I have been having problems with some of them.

The following code works, replied with 200:

import requests
query = {'lat':'45', 'lon':'180'}
print(str(query))
response = requests.get('http://api.open-notify.org/iss-pass.json', params=query)
print(response.json())

response = requests.get('http://api.open-notify.org/astros.json', params=query)
print(response.json())
print(response.status_code)

However, the next piece of code fails:

query = {'lat':'39.634461','lng':'-86.388907','formatted':'0'}
print(str(query))
response = requests.get('http://api.sunrise-sunset.org/json',params=query)
print(response.json())

Here is the error output. I do see an advisement in response.py about proactively flushing to work with jython, but don’t understand how it ends up in zlib.

TIA for any help

Traceback (most recent call last):
  File "<input>", line 22, in <module>
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\sessions.py", line 697, in send
    r.content
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\models.py", line 831, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\requests\models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\urllib3\response.py", line 572, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\urllib3\response.py", line 778, in read_chunked
    decoded = self._flush_decoder()
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\site-packages\urllib3\response.py", line 421, in _flush_decoder
    return buf + self._decoder.flush()
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\zlib.py", line 244, in flush
    last = _get_inflate_data(self.inflater, length)
  File "C:\Users\kmaze\.ignition\cache\gwusplatweng44354_8088\C0\pylib\zlib.py", line 281, in _get_inflate_data
    raise error(str(e))
zlib.error: java.util.zip.DataFormatException: invalid distance too far back

Not sure of what method you’ve used to install requests library into Ignition, perhaps that is related (an example test of running that with requests under CPython seems to be just fine). You should be able to get what you need with the built-in system.net.httpClient():

url = 'http://api.sunrise-sunset.org/json'
query = {'lat':'39','lng':'-86','formatted':'0'}
client = system.net.httpClient()
response = client.get(url, params=query)
print(response.json)

The above produces an expected result:

{
    "results": {
        "sunrise": "u'2021-04-28T10:48:14+00:00'",
        "solar_noon": "u'2021-04-28T17:41:21+00:00'",
        "day_length": "49575L",
        "astronomical_twilight_end": "u'2021-04-29T02:14:52+00:00'",
        "astronomical_twilight_begin": "u'2021-04-28T09:07:50+00:00'",
        "civil_twilight_end": "u'2021-04-29T01:03:04+00:00'",
        "nautical_twilight_end": "u'2021-04-29T01:37:47+00:00'",
        "sunset": "u'2021-04-29T00:34:29+00:00'",
        "civil_twilight_begin": "u'2021-04-28T10:19:39+00:00'",
        "nautical_twilight_begin": "u'2021-04-28T09:44:55+00:00'"
    },
    "status": "u'OK'"
}

I think the question is why it works for the first request but not the second one… I can confirm (with my installation of requests) that what @ken.maze is saying is also true for me.

I went through a whole procedure that I found in the forums, but seems to have disappeared. IIRC it was loading it into an external jython environment, and taking the resulting libraries into the site-packages directory

3rd Party python library installs

I checked you resolution, and it works, nevertheless would like to know why this solution doesn’t work. I have had pretty good luck using requests internally, and have a couple of solutions in production, but hadn’t used it to try to get some of these external APIs, so was concerned.

What version are you using? I am using 2.25.0. I see there is a newer version available, but don’t see anything in the release that looks like it would fix the problem.

It’s just a bug in requests and/or Jython, likely a combination of an old version + not being tested on Jython.

The server response is likely gzipped for efficiency over-the-wire, and that’s why zlib is being invoked to decode the response.

Seems like perhaps a bug in the behavior of requests in Jython… Looks like you can get around it by specifying the Accept-Encoding: none header, e.g.:

>>> import requests
... query = {'lat':'39','lng':'-86','formatted':'0'}
... print(str(query))
... headers = {'Accept-Encoding': 'none'}
... response = requests.get('http://api.sunrise-sunset.org/json',params=query, headers=headers)
... print(response.json())
... 
{'lng': '-86', 'formatted': '0', 'lat': '39'}
{u'results': {u'sunrise': u'2021-04-28T10:48:14+00:00', u'solar_noon': u'2021-04-28T17:41:21+00:00', u'day_length': 49575, u'astronomical_twilight_end': u'2021-04-29T02:14:52+00:00', u'astronomical_twilight_begin': u'2021-04-28T09:07:50+00:00', u'civil_twilight_end': u'2021-04-29T01:03:04+00:00', u'nautical_twilight_end': u'2021-04-29T01:37:47+00:00', u'sunset': u'2021-04-29T00:34:29+00:00', u'civil_twilight_begin': u'2021-04-28T10:19:39+00:00', u'nautical_twilight_begin': u'2021-04-28T09:44:55+00:00'}, u'status': u'OK'}
>>> 
1 Like

I also tried to set headers = {"Accept-Encoding": "gzip, deflate, br"} like Postman does by default, but I ended up getting the same error. I didn’t try None!

This is a good tip :+1:

Thank you gentlemen for the guidance, it worked for me! I appreciate the help!

Ken