From b96aaa79677635d2d81a0f1fda357903a5334918 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 31 Jan 2016 22:34:17 -0800 Subject: [PATCH] Added charset option for JSON requests. --- README.md | 2 +- VERSION | 2 +- flight/Engine.php | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 777d481..84c6290 100644 --- a/README.md +++ b/README.md @@ -723,7 +723,7 @@ $body = Flight::request()->getBody(); ## JSON Input -If you send request with the type `application/json` and the data `{"id": 123}` it will be available +If you send a request with the type `application/json` and the data `{"id": 123}` it will be available from the `data` property: ```php diff --git a/VERSION b/VERSION index 591bdbc..9ddbe16 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.18 +1.2.19 diff --git a/flight/Engine.php b/flight/Engine.php index 0823f3e..d55db3f 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -448,13 +448,14 @@ class Engine { * @param mixed $data JSON data * @param int $code HTTP status code * @param bool $encode Whether to perform JSON encoding + * @param string $charset Charset */ - public function _json($data, $code = 200, $encode = true) { + public function _json($data, $code = 200, $encode = true, $charset = 'utf-8') { $json = ($encode) ? json_encode($data) : $data; $this->response() ->status($code) - ->header('Content-Type', 'application/json') + ->header('Content-Type', 'application/json; charset='.$charset) ->write($json) ->send(); } @@ -466,15 +467,16 @@ class Engine { * @param string $param Query parameter that specifies the callback name. * @param int $code HTTP status code * @param bool $encode Whether to perform JSON encoding + * @param string $charset Charset */ - public function _jsonp($data, $param = 'jsonp', $code = 200, $encode = true) { + public function _jsonp($data, $param = 'jsonp', $code = 200, $encode = true, $charset = 'utf-8') { $json = ($encode) ? json_encode($data) : $data; $callback = $this->request()->query[$param]; $this->response() ->status($code) - ->header('Content-Type', 'application/javascript') + ->header('Content-Type', 'application/javascript; charset='.$charset) ->write($callback.'('.$json.');') ->send(); }