Mobrovac has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/388486 )

Change subject: WebRequest: Allow clients to POST JSONs in the body
......................................................................

WebRequest: Allow clients to POST JSONs in the body

The API's preferred way of sending parameters is via POST. Additionally,
some Special pages expect the payload to be POSTed as well. Therefore,
accept requests that have their data embedded as a JSON in the body.

Change-Id: I8285ddf56201ccbc6ada1353ea3f31b5cce72484
---
M includes/WebRequest.php
1 file changed, 22 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/86/388486/1

diff --git a/includes/WebRequest.php b/includes/WebRequest.php
index 3d5e372..f5c103b 100644
--- a/includes/WebRequest.php
+++ b/includes/WebRequest.php
@@ -93,7 +93,28 @@
 
                // POST overrides GET data
                // We don't use $_REQUEST here to avoid interference from 
cookies...
-               $this->data = $_POST + $_GET;
+               // Parse the data correctly if the client supplied it as a JSON 
object
+               // in the request's body
+               $headers = $this->getAllHeaders();
+               if ( $this->wasPosted() &&
+                               isset( $headers['CONTENT-TYPE'] ) &&
+                               preg_match( "/\bapplication\/json/", 
$headers['CONTENT-TYPE'] ) ) {
+                       $decoded = NULL;
+                       try {
+                               $decoded = json_decode( 
$this->getRawPostString(), true );
+                       } catch ( Exception $e ) {
+                               // the contents of the body cannot be decoded 
properly, so log the
+                               // exception, but continue as if the body was 
not there
+                               MWExceptionHandler::logException( $e );
+                       }
+                       if ( is_array( $decoded ) ) {
+                               $this->data = $decoded + $_GET;
+                       } else {
+                               $this->data = $_GET;
+                       }
+               } else {
+                       $this->data = $_POST + $_GET;
+               }
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/388486
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8285ddf56201ccbc6ada1353ea3f31b5cce72484
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Mobrovac <mobro...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to