API:Edit

This module is implemented by ApiEditPage.php.

Token
To edit a page, an edit token is required. This token is the same for all pages, but changes at every login. The preferred method to obtain an edit token depends on the MediaWiki version: Currently, all older methods continue to work, but are deprecated.
 * Versions 1.24 and later: action=query&meta=tokens
 * Versions 1.20-1.23: action=tokens
 * Versions 1.19 and earlier: action=query&prop=info

If you want to protect against edit conflicts (which is wise), you also need to get the timestamp of the last revision, included in the example below:

When passing this to the Edit API, always pass the token parameter last (or at least after the text parameter). That way, if the edit gets interrupted, the token won't be passed and the edit will fail. This is done automatically by mw.Api.

When running on a MediaWiki page, the token can be obtained by calling  (more information).

Unregistered users
For unregistered users, the token is always, but you may want to request it explicitly in case this changes in the future.

Editing pages
Pages can be edited and created with.

Example
Note: In this example, all parameters are passed in a GET request just for the sake of simplicity. However, action=edit requires POST requests; GET requests will cause an error. Do not forget to set the  header of your request to. The token that you received is terminated with, this needs to be urlencoded (so it will end with  ) before it is passed back.

CAPTCHAs and extension errors
If an edit requires a CAPTCHA, you'll get something like: The specific information you get will depend on the captcha in use. In most cases, you won't get a math CAPTCHA, but a URL to an image (in the  field). When you've solved the CAPTCHA, retry the request (in this case with ).

Other extensions that use the APIEditBeforeSave hook may return failure results using either the format described above or the usual error format.

Possible errors
In addition to the usual stuff:

Editing via Ajax
Below is sample code for editing a page via an Ajax request: You can also use the mw.Api object: Remember to make sure the  module is loaded when using this methodology.

Editing via Python

 * /Editing with Python

Large texts
When using texts over 8000 characters, use "multipart/form-data" as it does not require escaping and will be significantly smaller than its urlencoded counterpart. Multipart has roughly 160 bytes of overhead for each parameter, so for seven parameters that's ~1.1kb overhead. Example: POST /w/api.php HTTP/1.1 User-Agent: Bot Framework Content-Type: multipart/form-data; boundary=---8ce61ec834cf268 Host: en.wikipedia.org Cookie: Content-Length: 1348 Accept-Encoding: gzip Connection: Keep-Alive

-8ce61ec834cf268 Content-Disposition: form-data; name="action"

edit -8ce61ec834cf268 Content-Disposition: form-data; name="title" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Title here -8ce61ec834cf268 Content-Disposition: form-data; name="text" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Text here -8ce61ec834cf268 Content-Disposition: form-data; name="summary" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Summary -8ce61ec834cf268 Content-Disposition: form-data; name="token"

1f287ba00a908e9622045e7b18ffa352+\ -8ce61ec834cf268 Content-Disposition: form-data; name="assert"

user -8ce61ec834cf268 Content-Disposition: form-data; name="format"

xml -8ce61ec834cf268--