URL Requests change error response
Instead of just throwing exception on 401 auth, throw exception for any error code from 400 on This can be turned off with the option "http_errors" set to false Also updaed the exception content to match 400 or 500 error type with more information attached General Exception error codes: Cnnn: Curl errors (FAILURE) Rnnn: general class errors (ERROR) Hnnn: http response errors (ERROR)
This commit is contained in:
@@ -15,7 +15,7 @@ $log = new CoreLibs\Logging\Logging([
|
||||
/**
|
||||
* build return json
|
||||
*
|
||||
* @param array<string,mixed> $http_headers
|
||||
* @param array<string,mixed> $http_headers
|
||||
* @param string $body
|
||||
* @return string
|
||||
*/
|
||||
@@ -25,7 +25,8 @@ function buildContent(array $http_headers, string $body): string
|
||||
'HEADERS' => $http_headers,
|
||||
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
|
||||
"PARAMS" => $_GET,
|
||||
"BODY" => Json::jsonConvertToArray($body)
|
||||
"BODY" => Json::jsonConvertToArray($body),
|
||||
// "STRING_BODY" => $body,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -40,11 +41,15 @@ header("Content-Type: application/json; charset=UTF-8");
|
||||
// if the header has Authorization and RunAuthTest then exit with 401
|
||||
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
print buildContent($http_headers, '["code": 401, "content": {"Error" => "Not Authorized"}]');
|
||||
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
|
||||
exit;
|
||||
}
|
||||
|
||||
$file_get = file_get_contents('php://input') ?: '{"Error" => "file_get_contents failed"}';
|
||||
if (($file_get = file_get_contents('php://input')) === false) {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
|
||||
exit;
|
||||
}
|
||||
// str_replace('\"', '"', trim($file_get, '"'));
|
||||
|
||||
$log->debug('SERVER', $log->prAr($_SERVER));
|
||||
|
||||
@@ -244,7 +244,7 @@ print "<hr>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"exception_on_not_authorized" => false,
|
||||
"http_errors" => false,
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
@@ -262,7 +262,7 @@ print "AUTH REQUEST WITH EXCEPTION:<br>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"exception_on_not_authorized" => true,
|
||||
"http_errors" => true,
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
@@ -285,7 +285,7 @@ $uc = new Curl([
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php', ["headers" => null, "query" => ["test" => "one-test"]]);
|
||||
print "AUTH REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "HEADER RESET REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
|
||||
Reference in New Issue
Block a user