Policy:User-Agent policy: Difference between revisions

From Wikimedia Foundation Governance Wiki
Content deleted Content added
No edit summary
No edit summary
Line 16: Line 16:
<translate>
<translate>


== Code examples == <!--T:17-->
== JavaScript ==

<!--T:18-->
On Wikimedia wikis, if you don't supply a <tvar name="1"><code>User-Agent</code></tvar> header, or you supply an empty or generic one, your request will fail with an HTTP 403 error. Other MediaWiki installations may have similar policies.

=== JavaScript === <!--T:23-->

<!--T:19-->
If you are calling the API from browser-based JavaScript, you won't be able to influence the <tvar name="1"><code>User-Agent</code></tvar> header: the browser will use its own. To work around this, use the <tvar name="header-code"><code>Api-User-Agent</code></tvar> header:
</translate>

<syntaxhighlight lang="javascript">
// Using XMLHttpRequest
xhr.setRequestHeader( 'Api-User-Agent', 'Example/1.0' );
</syntaxhighlight>
<syntaxhighlight lang="javascript">
// Using jQuery
$.ajax( {
url: 'https://example/...',
data: ...,
dataType: 'json',
type: 'GET',
headers: { 'Api-User-Agent': 'Example/1.0' },
} ).then( function ( data ) {
// ..
} );
</syntaxhighlight>
<syntaxhighlight lang="javascript">
// Using mw.Api
var api = new mw.Api( {
ajax: {
headers: { 'Api-User-Agent': 'Example/1.0' }
}
} );
api.get( ... ).then( function ( data ) {
// ...
});
</syntaxhighlight>
<syntaxhighlight lang="javascript">
// Using Fetch
fetch( 'https://example/...', {
method: 'GET',
headers: new Headers( {
'Api-User-Agent': 'Example/1.0'
} )
} ).then( function ( response ) {
return response.json();
} ).then( function ( data ) {
// ...
});
</syntaxhighlight>

<translate>
=== PHP === <!--T:24-->

<!--T:20-->
In PHP, you can identify your user-agent with code such as this:
</translate>

<syntaxhighlight lang="php">
ini_set( 'user_agent', 'CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org)' );
</syntaxhighlight>

<translate>
=== cURL === <!--T:25-->

<!--T:21-->
Or if you use [[wikipedia:cURL|cURL]]:
</translate>

<syntaxhighlight lang="php">
curl_setopt( $curl, CURLOPT_USERAGENT, 'CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org)' );
</syntaxhighlight>

<translate>
=== Python === <!--T:26-->

<!--T:27-->
In Python, you can use the [[wikipedia:Requests_(software)|Requests]] library to set a header:
</translate>

<syntaxhighlight lang="python">
import requests

url = 'https://example/...'
headers = {'User-Agent': 'CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org)'}

response = requests.get(url, headers=headers)
</syntaxhighlight>

<translate>
<!--T:28-->
Or, if you want to use [<tvar name="url">https://sparqlwrapper.readthedocs.io</tvar> SPARQLWrapper] like in <tvar name="url2">https://people.wikimedia.org/~bearloga/notes/wdqs-python.html</tvar>:
</translate>

<syntaxhighlight lang="python">
from SPARQLWrapper import SPARQLWrapper, JSON

url = 'https://example/...'
user_agent = 'CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org)'

sparql = SPARQLWrapper(url, agent = user_agent )
results = sparql.query()
</syntaxhighlight>

<translate>

== Notes == <!--T:12-->
== Notes == <!--T:12-->
</translate>
</translate>

Revision as of 11:58, 17 November 2023

As of February 15, 2010, Wikimedia sites require aheader for all requests. This was an operative decision made by the technical staff and was announced and discussed on the technical mailing list. The rationale is, that clients that do not send a User-Agent string are mostly ill behaved scripts that cause a lot of load on the servers, without benefiting the projects. User-Agent strings that begin with non-descriptive default ).

Requests (e.g. from browsers or scripts) that do not send a descriptive User-Agent header, may encounter an error message like this:

Scripts should use an informative User-Agent string with contact information, or they may be blocked without notice.

Requests from disallowed user agents may instead encounter a less helpful error message like this:

Our servers are currently experiencing a technical problem. Please try again in a few minutes.

This change is most likely to affect scripts (bots) accessing Wikimedia websites such as Wikipedia automatically, via api.php or otherwise, and command line programs. If you run a bot, please send a User-Agent header identifying the bot with an identifier that isn't going to be confused with many other bots, and supplying some way of contacting you (e.g. a userpage on the local wiki, a userpage on a related wiki using interwiki linking syntax, a URI for a relevant external website, or an email address), e.g.:


JavaScript

Notes


See also