<p>In Craft3, the correct way of setting HTTP status codes is </p>

<p><code>Craft::$app-&gt;getResponse()-&gt;setStatusCode(404);</code></p>

CC BY-SA 4.0
2019-04-04T10:46:48.897Z
2019-04-04T10:46:48.897Z
pabloroman
<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>

CC BY-SA 3.0
2015-01-09T00:46:39.817Z
2015-01-09T00:46:39.817Z
Brad Bell
<pre><code>HeaderHelper::setHeader(array('status' =&gt; 404));
</code></pre>

CC BY-SA 3.0
2016-11-09T21:41:47.003Z
2016-11-09T21:41:47.003Z
Brad
<pre><code>http_response_code(404);
return $this-&gt;returnErrorJson('Something happened, try again.');
</code></pre>

CC BY-SA 4.0
2021-03-12T13:15:00.953Z
2021-03-12T13:15:00.953Z
Brad
<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>

<plugin-development><controller><ajax>
How can I set the HTTP status code?
# 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.');


---

