Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONDecodeError #3

Open
scanf13ld opened this issue Jul 6, 2020 · 1 comment
Open

JSONDecodeError #3

scanf13ld opened this issue Jul 6, 2020 · 1 comment

Comments

@scanf13ld
Copy link

Hello! I am getting this error when trying to use this method. It was working before back in early 2019, but for some reason it isn't working anymore. I recently regenerated an API key for my account and have access to the basic API functions.

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-68-0abce5d0bc4f> in <module>()
----> 1 cand = crp.candidates.get('N00007360') #N00007360 corresponds to Nancy Pelosi's crp_id - a unique ID provided by the API
      2 cand['@attributes']['firstlast']

4 frames
/usr/local/lib/python3.6/dist-packages/crpapi.py in get(self, id_code)
     67             state code, or a four character district code.
     68         """
---> 69         return self.fetch('getLegislators', id=id_code)['legislator']
     70 
     71     def pfd(self, cid, year=None):

/usr/local/lib/python3.6/dist-packages/crpapi.py in fetch(self, method, **kwargs)
     48 
     49         resp, content = self.http.request(url, headers=headers)
---> 50         content = json.loads(content)
     51 
     52         if not resp.get('status') == '200':

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
@grumble1965
Copy link

I've got a similar issue. When I request data that doesn't exist (like, a candidate's industry contributions from an industry they haven't got any contributions from), I also get a JSON decode error.

I think the issue is the fetch() method trying to decode the http response before checking the http response status. There's no JSON to decode if the response isn't 200.

Here's my fix:
Untitled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants