Wikimedia

Return HTTP 405 error code when unsupported http method used with MediaWiki action API

The goal of this task is to respond with a 405 status code, when people use unsupported HTTP methods with MediaWiki's ActionAPI.

For example, consider the following command line command under Linux systems:

curl -X DELETE 'https://en.wikipedia.org/w/api.php?action=query&prop=info&titles=Main_Page&format=json'

responds as if the user specified GET instead of DELETE. This is confusing.

To change this:

  • (If you haven't already) Download & setup a local copy of MediaWiki ( https://www.mediawiki.org/wiki/MediaWiki-Vagrant )
  • In mediawiki core, open up includes/api/ApiMain.php. Locate the setupExternalResponse method
  • Near where the check is for POST, add another check. This check should check if the method is one of GET, HEAD, POST or OPTIONS (perhaps using an if ( !in_array(... check). See also the documentation
  • In the event the method is one other than the allowed method, you should call $this->dieWithError(...). The fourth argument should be 405 to return a 405 HTTP status code
  • Add the message (starting with apierror-) you used in the call to dieWithError() to includes/api/i18n/en.json and includes/api/i18n/qqq.json (This will contain message documentation telling translators where this message appears). Do not worry about other translations, they are handled by translatewiki)
  • Test your change.
  • You are expected to provide a patch in Wikimedia Gerrit. See https://www.mediawiki.org/wiki/Gerrit/Tutorial for how to set up Git and Gerrit.

Task tags

  • api
  • http
  • error handling

Students who completed this task

Shreyas Minocha

Task type

  • code Code
close

2018