【发布时间】:2015-03-23 13:50:15
【问题描述】:
我正在开发一个 RESTful Web 服务,老实说,这是我的第一个 ws。我决定使用 PHP,因为我认为我知道那种语言。
这是我的 RestHandler 对象,但是当我调试请求时,使用 Charles 访问未实现的方法时,它返回正确的响应,但 500 错误而不是 400。为什么?
class RestHandler {
private $method;
private $actionName;
/**
* @param $method
* @param $action
*/
public function __construct($method, $action)
{
$this->method = $method;
$this->actionName = $action;
if (isset($this->method) && isset($this->actionName))
{
if (! method_exists($this, $this->actionName))
{
// Action is not implemented in the object.
$this->handleErrorReturning("Not implemented method.", 400);
return;
}
// OK, proceed with actions
$this->handleProceedRequest();
}
else
{
// Return error 406 Missing parameter
$this->handleErrorReturning("Missing parameter", 406);
}
}
private function handleProceedRequest()
{
if (strcasecmp($this->method, "get") == 0)
{
// No JSON to read
}
}
/**
* @param $errorDescription
* @param $errorCode
*/
private function handleErrorReturning($errorDescription, $errorCode)
{
header($_SERVER["SERVER_PROTOCOL"]." ".$errorDescription." ".$errorCode);
header('Content-Type: application/json; charset=utf-8');
$errorResponse = new ResponseError($errorCode, $errorDescription);
echo $errorResponse;
}
}
这是查尔斯的快照
已解决 我用errorCode反转了errorDescription,现在它可以工作了。这是一个愚蠢的错误。谢谢
【问题讨论】:
-
对不起,这是一个网络服务
标签: php rest http-headers http-status-code-400