Until T282259 is resolved, the MediaWiki REST API does not support the Api-User-Agent header. For now, let's remove the Api-User-Agent header from JavaScript examples in mw:API:REST API/Reference to reflect the current state.
For example:
async function doFetch() { const rsp = await fetch( "https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20", {'Api-User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'} ); 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();
At a minimum, this should be changed to:
async function doFetch() { const rsp = await fetch( "https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20", ); 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();
However, additional improvements to this sample code are very welcome! For example, the following code is clearer and in better alignment with mw:Manual:Coding_conventions/JavaScript:
let url = 'https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20'; let response = await fetch( url ); response.json() .then(console.log).catch(console.error);
Code samples on mw:API:REST API/Reference are transcluded from subpages. API:REST_API/About provides an index of subpages used in these docs.
This bug was originally spotted in T268791#7066560.