From 0d6abb53d589a35119fbaed0ece9ee4466d7c996 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 16 Sep 2022 13:58:09 +0200 Subject: [PATCH] Added return data to api request debugging --- lib/mvcfronk/mfBase/mfBaseApicontroller.php | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/mvcfronk/mfBase/mfBaseApicontroller.php b/lib/mvcfronk/mfBase/mfBaseApicontroller.php index b4a6ed3de..2425c85e8 100644 --- a/lib/mvcfronk/mfBase/mfBaseApicontroller.php +++ b/lib/mvcfronk/mfBase/mfBaseApicontroller.php @@ -77,6 +77,7 @@ class mfBaseApicontroller { } private function logRequest() { + $this->requestLog->debug("=================================================================="); $this->requestLog->debug("new API request for ".$_SERVER['REQUEST_URI']); $this->requestLogstr = ""; foreach($_GET as $key => $value) { @@ -162,7 +163,7 @@ class mfBaseApicontroller { private function getPostRequest() { $body = $this->getRequestBody(); - if(is_array($body)) { + if(!is_string($body) && is_array($body)) { // request is parsed already ($_POST) return $body; } @@ -179,7 +180,7 @@ class mfBaseApicontroller { } private function getRequestBody() { - if($_SERVER["CONTENT_TYPE"] == "application/json") { + if(strtolower($this->headers['content-type']) == "application/json") { $request_body = file_get_contents('php://input'); return $request_body; } @@ -208,6 +209,17 @@ class mfBaseApicontroller { if($_SERVER["SERVER_PROTOCOL"]) { $proto = $_SERVER["SERVER_PROTOCOL"]; } + + $this->requestLog->debug("$proto $code $status"); + $this->requestLog->debug("status $status, result: "); + foreach($data as $key => $res) { + if(is_array($res)) { + $this->requestLog->debug($key.": (".count($res).")"); + } else { + $this->requestLog->debug($key.": ".$res); + } + } + header("$proto $code $status"); header("Content-type: application/json"); //http_response_code($code); @@ -235,6 +247,19 @@ class mfBaseApicontroller { if($_SERVER["SERVER_PROTOCOL"]) { $proto = $_SERVER["SERVER_PROTOCOL"]; } + + $log = new mfLog_File(); + $log->init(BASEDIR."/var/log/api-request.log"); + $log->debug("$proto $code $status"); + $log->debug("status $status, result: "); + foreach($data as $key => $res) { + if(is_array($res)) { + $log->debug($key.": (".count($res).")"); + } else { + $log->debug($key.": ".$res); + } + } + header("$proto $code $status"); header("Content-type: application/json"); //http_response_code($code); @@ -361,7 +386,7 @@ class mfBaseApicontroller { } // no route found - $this->return(mfResponse::BadRequest()); + $this->return(mfResponse::BadRequest(["message" => "Invalid endpoint"])); exit; }