Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2中Magento Rest API中的常见错误HTTP代码

今天我将向您介绍Magento 2 Rest API中的常见错误HTTP代码。首先,您还需要知道HTTP是什么?HTTP代表超文本传输​​协议,网站上的默认协议。在使用该协议期间,可能存在一些常见错误,称为HTTP错误代码。

为了更好地理解,下面包括Magento 2 Rest API中的每个常见错误HTTP代码的代码列表和含义。

HTTP错误400

  • 常量: Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
  • 描述:请求数据无效。

HTTP错误401

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_UNAUTHORIZED
  • 说明:授权失败。

HTTP错误403

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_FORBIDDEN
  • 说明:拒绝访问。

HTTP错误404

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_NOT_FOUND
  • 描述:未找到资源。

HTTP错误405

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_METHOD_NOT_ALLOWED
  • 描述:资源不支持方法。/资源方法尚未实现。

HTTP错误406

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_NOT_ACCEPTABLE
  • 描述:请求的资源不接受请求。

HTTP错误500

  • 常量:Magento \ Framework \ Webapi \ Exception :: HTTP_INTERNAL_ERROR
  • 描述:资源内部错误。

接下来,我将举例说明一些错误的HTTP代码。

如果您在API方法中运行打印异常代码:

if (!$productId) {
 
throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested product doesn\'t exist'));
 
}

那时,HTTP错误404将显示在响应中。

或者您可以使用Magento提供的异常类Magento\Framework\Exception,或使用常规异常类\ Exception:

if (!$productId) {
 
throw new \Exception(__('Requested product doesn\'t exist'));
 
}

确保错误代码始终为500(Magento \ Framework \ Webapi \ Exception :: HTTP_INTERNAL_ERROR)。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论