mediawiki-api-demos icon indicating copy to clipboard operation
mediawiki-api-demos copied to clipboard

response.json() produces JSONDecodeError error since content is in invalid format

Open pcnoic opened this issue 4 years ago • 0 comments

https://github.com/wikimedia/mediawiki-api-demos/blob/9a5ac610bf79dbada23cce74972c523fee847199/apps/client-login/clientlogin.py#L53

Bug

When trying to work with the clientlogin method and using the default example from the documentation the user may encounter an issue where the response.json() produces json encoding error in the runtime. For example a common error would be:

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Expected behavior

Received the response that is documented in the docs:

{  
   "clientlogin":{  
      "status":"PASS",
      "username":"William"
   }
}

Workaround

I'd suggest parsing the response using:

data = json.loads(response.content)

instead of:

data = response.json()

pcnoic avatar Jun 21 '21 11:06 pcnoic