Permalink
Browse files

Merge pull request #45 from jeffgirard/master

Added support to POST request containing a body formatted as JSON
  • Loading branch information...
2 parents 334fa42 + 12e79b7 commit cdad449eea53a08517b0d6889b9875a36d24c018 @16 16 committed Oct 28, 2013
Showing with 23 additions and 3 deletions.
  1. +8 −2 lib/limonade.php
  2. +9 −1 tests/apps/08-params.php
  3. +6 −0 tests/functional.php
View
@@ -515,9 +515,15 @@ function env($reset = null)
}
$method = request_method($env);
- if($method == 'PUT' || $method == 'DELETE')
+
+ $varname = "_$method";
+ if (strpos($_SERVER['CONTENT_TYPE'], 'application/json') === 0)
+ {
+ // handle PUT/POST requests which have JSON in request body
+ $GLOBALS[$varname] = json_decode(file_get_contents('php://input'), true);
+ }
+ elseif($method == 'PUT' || $method == 'DELETE')
{
- $varname = "_$method";
if(array_key_exists('_method', $_POST) && $_POST['_method'] == $method)
{
foreach($_POST as $k => $v)
@@ -19,6 +19,14 @@ function books()
return $o;
}
+dispatch_post('/books', 'create');
+ function create()
+ {
+ $o = '';
+ if(array_key_exists('title', $_POST)) $o = "title=" . $_POST['title'];
+ return $o;
+ }
+
-run();
+run();
@@ -126,6 +126,12 @@ function test_functional_params()
$response = test_request($path.'?uri=books/fr&sort=asc&page=2', 'GET');
assert_match('/sort=asc/', $response);
assert_match('/page=2/', $response);
+
+ $post = json_encode(array('title' => 'hello world'));
+ $headers = array('Content-Type: application/json',
+ 'Content-Length: ' . strlen($post));
+ $response = test_request($path.'?uri=books', 'POST', false, $post, $headers);
+ assert_match('/title=hello world/', $response);
}

0 comments on commit cdad449

Please sign in to comment.