Created
September 17, 2012 15:58
-
-
Save DracoBlue/3738178 to your computer and use it in GitHub Desktop.
Agavi patch to allow json payload in AgaviWebRequest + allow PATCH-method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: agavi/request/AgaviWebRequest.class.php | |
=================================================================== | |
--- agavi/request/AgaviWebRequest.class.php (revision 1086) | |
+++ agavi/request/AgaviWebRequest.class.php (working copy) | |
@@ -343,6 +343,7 @@ | |
$methods = array_merge(array( | |
'GET' => 'read', | |
'POST' => 'write', | |
+ 'PATCH' => 'patch', | |
'PUT' => 'create', | |
'DELETE' => 'remove', | |
), (array)$this->getParameter('method_names')); | |
@@ -462,6 +463,15 @@ | |
) | |
); | |
} | |
+ } elseif(in_array($this->getMethod(), array($methods['POST'], $methods['DELETE'], $methods['PUT'], $methods['PATCH'])) && isset($_SERVER['CONTENT_TYPE']) && preg_match('#^(application/json|text/json)(;[^;]+)*?$#', $_SERVER['CONTENT_TYPE'])) { | |
+ // a valid request and a JSON Payload | |
+ $json_content = file_get_contents('php://input'); | |
+ $decoded_json_content = @json_decode($json_content, true); | |
+ if (is_array($decoded_json_content)) { | |
+ $_POST = array_merge($_POST, $decoded_json_content); | |
+ } else { | |
+ // invalid json, so we'll ignore it - one could sent a 400 bad request here. | |
+ } | |
} elseif($this->getMethod() == $methods['POST'] && (!isset($_SERVER['CONTENT_TYPE']) || (isset($_SERVER['CONTENT_TYPE']) && !preg_match('#^(application/x-www-form-urlencoded|multipart/form-data)(;[^;]+)*?$#', $_SERVER['CONTENT_TYPE'])))) { | |
// POST, but no regular urlencoded data or file upload. lets put the request payload into a file | |
$postFile = tempnam(AgaviConfig::get('core.cache_dir'), 'POSTUpload_'); | |
@@ -534,4 +544,4 @@ | |
} | |
} | |
-?> | |
\ No newline at end of file | |
+?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment