【问题标题】:error 500 setting header to return 400 error PHP错误 500 设置标头返回 400 错误 PHP
【发布时间】: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


【解决方案1】:

尝试像这样设置标题:

header($_SERVER['SERVER_PROTOCOL'] . ' Not implemented method', true, 400);

【讨论】:

  • 嗨,我解决了在标题中使用 errorDescr 反转 errorCode。这是一个愚蠢的错误。谢谢
猜你喜欢
  • 2017-09-30
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多