Skip to content

Commit 6c4393f

Browse files
committed
Interpret PUT requests with JSON or XML contents
1 parent c01f37a commit 6c4393f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/WaterPipe/Routing/Router.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,31 @@ private function _detectUri()
141141

142142
$data = array();
143143

144-
if ($this->_request->getMethod() === RequestMethod::PUT) {
144+
$contentType = trim(explode(";", $this->_request->getHeader()->getContentType())[0]);
145+
146+
if (count($_POST) > 0) {
147+
$data = $_POST;
148+
} elseif ($this->_request->getMethod() === RequestMethod::PUT) {
145149
$handle = fopen("php://input", "r");
146150
$rawData = '';
147151
while ($chunk = fread($handle, 1024)) {
148152
$rawData .= $chunk;
149153
}
150154

151-
parse_str($rawData, $data);
152-
} elseif (count($_POST) > 0) {
153-
$data = $_POST;
155+
switch ($contentType) {
156+
case "application/json":
157+
$data = json_decode($rawData, true);
158+
break;
159+
case "application/xml":
160+
$data = (array)simplexml_load_string($rawData);
161+
break;
162+
default:
163+
parse_str($rawData, $data);
164+
break;
165+
}
154166
} elseif ($this->_request->getMethod() !== RequestMethod::GET) {
155167
$rawData = file_get_contents("php://input");
156168

157-
$contentType = trim(explode(";", $this->_request->getHeader()->getContentType())[0]);
158169
switch ($contentType) {
159170
case "application/json":
160171
$data = json_decode($rawData, true);

0 commit comments

Comments
 (0)