Template:REST API Reference sample code (GET)
From Linux Web Expert
curl
$ curl "https://en.wikipedia.org/w/rest.php/some/path"
Python
import requests
url = 'https://en.wikipedia.org/w/rest.php/some/path'
headers = {
'User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'
}
response = requests.get(url, headers=headers)
data = response.json()
print(data)
PHP
<?php
$url = "https://en.wikipedia.org/w/rest.php/some/path";
$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
async function doFetch() {
let url = "https://en.wikipedia.org/w/rest.php/some/path";
let headers = {'Api-User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'}
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();
Template documentation
[create]