Answers.Body: <p>In Craft3, the correct way of setting HTTP status codes is </p>
  
  <p><code>Craft::$app-&gt;getResponse()-&gt;setStatusCode(404);</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-04T10:46:48.897Z
Answers.LastActivityDate: 2019-04-04T10:46:48.897Z
Answers.UserDisplayName: pabloroman
Answers.Body: <p>I believe <a href="http://buildwithcraft.com/classreference/helpers/HeaderHelper#setHeader-detail" rel="noreferrer">HeaderHelper::setHeader()</a> is what you're looking for in the context of a plugin.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-09T00:46:39.817Z
Answers.LastActivityDate: 2015-01-09T00:46:39.817Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <pre><code>HeaderHelper::setHeader(array('status' =&gt; 404));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-09T21:41:47.003Z
Answers.LastActivityDate: 2016-11-09T21:41:47.003Z
Answers.UserDisplayName: Brad
Answers.Body: <pre><code>http_response_code(404);
  return $this-&gt;returnErrorJson('Something happened, try again.');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-12T13:15:00.953Z
Answers.LastActivityDate: 2021-03-12T13:15:00.953Z
Answers.UserDisplayName: Brad
Body: <p>I'm trying to set a 404 HTTP status response code for an ajax controller action in a plugin.</p>
  
  <p>Yii has a response object where the code can be set, but I can't find a way to access it from craft.</p>
  
Tags: <plugin-development><controller><ajax>
Title: How can I set the HTTP status code?
ThreadText: # How can I set the HTTP status code?
  
  (Asked by: username_0 on 2015-01-09)
  
  I'm trying to set a 404 HTTP status response code for an ajax controller action in a plugin.
  
  Yii has a response object where the code can be set, but I can't find a way to access it from craft.
  
  **Tags:** plugin-development, controller, ajax
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2019-04-04. Score: 6
  
  In Craft3, the correct way of setting HTTP status codes is
  
  `Craft::$app->getResponse()->setStatusCode(404);`
  
  
  ---
  
  ### Answer by username_2 on 2015-01-09. Score: 6
  
  I believe [HeaderHelper::setHeader()](http://buildwithcraft.com/classreference/helpers/HeaderHelper#setHeader-detail) is what you're looking for in the context of a plugin.
  
  
  ---
  
  ### Answer by username_3 on 2016-11-09. Score: 3
  
  ```
  HeaderHelper::setHeader(array('status' => 404));
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2021-03-12. Score: 1
  
  ```
  http_response_code(404);
  return $this->returnErrorJson('Something happened, try again.');
  
  ```
  
  
  ---
  
  