Setting status of HTTP request
<p>I tried to set the http status in my custom API when a request is being made.</p>

<pre><code>protected $statusCode = 200;

public function setStatusCode($statusCode) {
        $this-&gt;statusCode = $statusCode;
        return $this;
    }

public function respond($data, $headers = []) {
        return response()-&gt;json($data, $this-&gt;getStatusCode(), $headers);
    }

public function respondCreated($message) {
    return $this-&gt;setStatusCode(201)-&gt;respond([
        'message' =&gt; $message
    ]);
}

$this-&gt;respondCreated("Incident was created");
</code></pre>

<p>But when I make my server request in POSTMAN, I see status 200 and not 201 as set in the code above and the message is not appearing at all. Do I need to do it differently?</p>

<p>I am using the Laravel framework and implemented the functions by the book "Build APIs you won't hate" </p>

<p>I used the <code>http_response_code()</code> method as suggested and set the code like this:</p>

<pre><code>public function respondCreated($message) {
    $this-&gt;setStatusCode(201)-&gt;respond([
        'message' =&gt; $message
    ]);
    http_response_code(201);
    return $this;
}
</code></pre>

<p>When I then return the response code it shows properly, but the POSTMAN Status is still 200?</p>

<p>The helper method by laravel is <code>response()</code> and is described as:</p>

<pre><code>Returning a full Response instance allows you to customize the response's HTTP status code and headers. A Response instance inherits from the Symfony\Component\HttpFoundation\Response class, providing a variety of methods for building HTTP responses:

use Illuminate\Http\Response;

Route::get('home', function () {
    return (new Response($content, $status))
                  -&gt;header('Content-Type', $value);
});
For convenience, you may also use the response helper:

Route::get('home', function () {
    return response($content, $status)
                  -&gt;header('Content-Type', $value);
});
</code></pre>

<p><a href="https://i.stack.imgur.com/V5iZF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V5iZF.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/5jSpV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5jSpV.png" alt="enter image description here"></a></p>