非请求所需数据的合适 HTTP 错误是什么

What's the suitable HTTP error for non requested required data

在我的例子中不需要任何参数。换句话说,应用程序不希望通过 post、get 或 put 获取任何数据。然而,在实现中,应该首先定义会话变量中存储的项目列表,例如购物车项目,用户收集项目,然后转到下面的操作,否则,它应该抛出 HTTP 错误。查看下面的示例:

public function actionCreate(){
  if(count(Yii::$app->session->get('versesList',[])) > 0){
    // Do the logic
  }
  else{
    throw new \yii\web\BadRequestHttpException(Yii::t('app', 'You must collect some verses first!')); 
  }
}

我不太清楚,400 错误请求是否是正确的错误代码。我曾尝试阅读维基百科上的 List of HTTP status codes,但我对其他一些 HTTP 错误感到困惑,例如:406 Not Acceptable412 Precondition Failed422 Unprocessable Entity

HTTP 400,因为您正在请求服务器不知道如何处理的操作(所以这是一个错误的请求)。

RFC2731 声明如下:

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

400 绝对适合这种情况,但如果您想更具体一点:

听起来用户需要一些其他 HTTP 请求 才能使此 HTTP 请求有效。对于这些情况,通常使用 409 Conflict

我在我的博客上写了更多关于这个的例子,如果它有趣的话:https://evertpot.com/http/409-confict