API:REST API/Reference/Sample code:Get page history

From mediawiki.org

curl[edit]

# Get revisions made to the Jupiter article on English Wikipedia by bots prior to revision 939967546
$ curl "https://en.wikipedia.org/w/rest.php/v1/page/Jupiter/history?filter=bot&older_than=939967546"


Python[edit]

# Get revisions made to the Jupiter article on English Wikipedia by bots prior to revision 939967546
import requests

url = 'https://en.wikipedia.org/w/rest.php/v1/page/Jupiter/history'
headers = {
    'User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'
}
params = {
    'filter': 'bot',
    'older_than': '939967546'
}

response = requests.get(url, headers=headers, params=params)
data = response.json()

print(data)


PHP[edit]

<?php
/*
Get revisions made to the Jupiter article on English Wikipedia by bots prior to revision 939967546
*/
$url = "https://en.wikipedia.org/w/rest.php/v1/page/Jupiter/history";
$params = [ "filter" => "bot", "older_than" => "939967546" ];
$url = $url . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, "MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)" );
$output = curl_exec( $ch );
curl_close( $ch );

echo($output);
?>


JavaScript[edit]

/*  
    Get revisions made to the Jupiter article on English Wikipedia by bots prior to revision 939967546
*/
async function doFetch() {
  let url = "https://en.wikipedia.org/w/rest.php/v1/page/Jupiter/history";
  let headers = {'Api-User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'}
  let params = {
    'filter': 'bot',
    'older_than': '939967546'
  };
  let query = Object.keys(params)
             .map(k => k + '=' + encodeURIComponent(params[k]))
             .join('&');
  url = url + '?' + query;

  const rsp = await fetch(url, headers);
  const data = await rsp.json();
  return data;
}

async function fetchAsync()
{
  try {
    let result = await doFetch();
    console.log(result);
  } catch( err ) {
    console.error( err.message );
  }
}

fetchAsync();