Request Body encoding + better method handling
This commit is contained in:
parent
a5e4cf6a74
commit
623e5dbbc8
@ -215,17 +215,40 @@ abstract class Request {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->isMethodAllowed("GET") && $this->isMethodAllowed("POST")) {
|
||||||
$values = $_REQUEST;
|
$values = $_REQUEST;
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array("application/json", explode(";", $_SERVER["CONTENT_TYPE"] ?? ""))) {
|
} else if ($this->isMethodAllowed("POST")) {
|
||||||
$jsonData = json_decode(file_get_contents('php://input'), true);
|
$values = $_POST;
|
||||||
|
} else if ($this->isMethodAllowed("GET")) {
|
||||||
|
$values = $_GET;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT', 'PATCH'])) {
|
||||||
|
$contentTypeData = explode(";", $_SERVER["CONTENT_TYPE"] ?? "");
|
||||||
|
$charset = "utf-8";
|
||||||
|
|
||||||
|
if ($contentTypeData[0] === "application/json") {
|
||||||
|
for ($i = 1; $i < count($contentTypeData); $i++) {
|
||||||
|
if (preg_match("/charset=(.*)/", $contentTypeData[$i], $match)) {
|
||||||
|
$charset = $match[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = file_get_contents('php://input');
|
||||||
|
if (strcasecmp($charset, "utf-8") !== 0) {
|
||||||
|
$body = iconv($charset, 'utf-8', $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonData = json_decode($body, true);
|
||||||
if ($jsonData !== null) {
|
if ($jsonData !== null) {
|
||||||
$values = array_merge($values, $jsonData);
|
$values = array_merge($values, $jsonData);
|
||||||
} else {
|
} else {
|
||||||
$this->lastError = 'Invalid request body.';
|
$this->lastError = "Invalid request body.";
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||||
http_response_code(204); # No content
|
http_response_code(204); # No content
|
||||||
@ -339,8 +362,7 @@ abstract class Request {
|
|||||||
$obj = $this->params;
|
$obj = $this->params;
|
||||||
}
|
}
|
||||||
|
|
||||||
// I don't know why phpstorm
|
return $obj[$name]?->value;
|
||||||
return (isset($obj[$name]) ? $obj[$name]->value : NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isMethodAllowed(string $method): bool {
|
public function isMethodAllowed(string $method): bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user