Home > Software design >  Mediawiki api returning http response code instead of json
Mediawiki api returning http response code instead of json

Time:11-02

I am following some example code in the mediawiki api (see link at the bottom) to get a response in json format that looks something like the following:

   "login": {  
      "lguserid": 21,
      "result": "Success",
      "lgusername": "William"
   }

Unfortunately, I am not getting anything similar to that and instead just get a response that looks as follows:

<Response [200]>

Below is the example code that I'm almost using verbatim at this point, to no avail.

#!/usr/bin/python3

"""
    login.py

    MediaWiki API Demos
    Demo of `Login` module: Sending post request to login
    MIT license
"""

import requests

USERNAME = "my_bot_username"
PASSWORD = "my_bot_password"

S = requests.Session()

URL = "https://www.mediawiki.org/w/api.php"

# Retrieve login token first
PARAMS_0 = {
    'action':"query",
    'meta':"tokens",
    'type':"login",
    'format':"json"
}

R = S.get(url=URL, params=PARAMS_0)

Again, R is supposed to be in json format instead it is just a http response code not in json format which causes the very next line of the example script (see link at the bottom) fail.

Is the example code outdated or can anyone spot why the code above is not returning a response in json format?

EDIT: The version of mediawiki I am using is 1.17.0

https://www.mediawiki.org/wiki/API:Login#Python

CodePudding user response:

You have to use response.json() to get a json object :

R = S.get(url=URL, params=PARAMS_0)
DATA = R.json()
  • Related