API:REST API/Reference/Sample code:Update page
< API:REST API | Reference
curl
edit# Update the sandbox page on English Wikipedia with "Hello, world!"
$ curl -X PUT https://en.wikipedia.org/w/rest.php/v1/page/Wikipedia:Sandbox -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" --data '{"source": "Hello, world!", "comment": "Testing out the REST API", "latest": { "id": 555555555 }}'
Python
edit# Update the sandbox page on English Wikipedia with "Hello, world!"
import requests
import json
url = "https://en.wikipedia.org/w/rest.php/v1/page/Wikipedia:Sandbox"
# Substitute your OAuth token
headers = {
"User-Agent" : "MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)",
"Content-Type" : "application/json",
"Authorization" : "Bearer $TOKEN"
}
# Use the get page endpoint to get the latest revision ID
request_data = {
"source" : "Hello, world!",
"comment": "Testing out the REST API",
"latest" : { "id": 555555555 }
}
response = requests.put( url, headers=headers, data = json.dumps(request_data) )
output = response.json()
print(output)
PHP
edit<?php
/*
Update the sandbox page on English Wikipedia with "Hello, world!"
*/
$page = 'Wikipedia:Sandbox';
$endpoint = 'https://en.wikipedia.org/w/rest.php/v1/page/';
$url = $endpoint . $page;
// Use the get page endpoint to get the latest revision ID
$fields = [
'source' => 'Hello, world!',
'comment' => 'Testing out the REST API',
'latest' => [
'id' => 555555555
]
];
$json = json_encode( $fields );
$token = 'YOUR_OAUTH_TOKEN'; // Substitute your OAuth token
$authorization = 'Authorization: Bearer ' . $token;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt( $ch, CURLOPT_USERAGENT, 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
echo( $output );
?>
JavaScript
edit/*
Update the sandbox page on English Wikipedia with "Hello, world!"
Substitute your OAuth token for $TOKEN.
Use the get page endpoint to get the latest revision ID.
*/
async function doFetch() {
const response = await fetch(
"https://en.wikipedia.org/w/rest.php/v1/page/Wikipedia:Sandbox",
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $TOKEN'
},
body: JSON.stringify({
"source": "Hello, world!",
"comment": "Testing out the REST API",
"latest": { "id": 555555555 }
}),
'Api-User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'
}
);
const data = await response.json();
return data;
}
async function fetchAsync()
{
try {
let result = await doFetch();
console.log(result);
} catch( err ) {
console.error( err.message );
}
}
fetchAsync();