# How to return Response with custom status code and message as JSON for response body with Slim Framework

**URL:** <https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202>\
**Category:** Questions\
**Created:** [February 27, 2017, 7:31am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202 "2017-02-27T07:31:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ywy9876](https://avatars.discourse-cdn.com/v4/letter/y/48db29/32.png) [@ywy9876](https://discourse.slimframework.com/u/ywy9876)\
**Post date:** [February 27, 2017, 7:31am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/1 "2017-02-27T07:31:35Z")

</div>

Hi,  
I’m new with Slim framework. I want to know how to return response with the body and status code as I wish. For now, it only performs as I wish if the query returns records, but not if the query failed (e.g. select \* from notexiststable) or returns nothing and with Firefox I see the status is 204 in both cases.

So here’s the code:

```auto
$app->get('/models', function (Request $request, Response $response) {
    $conn = getConnection();
    $result = pg_query($conn, "SELECT * FROM model where m_id = 2;"); //this will return 0 record!

    if (!$result) {
        $data = array("Error Message" => 'Query did not execute successfully');
        $newResponse = $response->withJson($data, 500);
    }
    else {
        if (pg_num_rows($result) == 0) {
            $data = array("Warning Message" => "There's no existent Model!");
            $newResponse = $response->withJson($data, 204);

        }
        else {
            $data = array();
            while ($row = pg_fetch_assoc($result)) {
                $data['Models'][] = $row;               
            }
            $newResponse = $response->withJson($data, 200);
        }
    }

    return $newResponse;

});

```

Thank you in advanced

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 27, 2017, 7:33am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/2 "2017-02-27T07:33:44Z")

</div>

@ywy9876 How are you testing a failure?

The only way to get a `500` response if if the `model` table does not exist in your database _and_ the result of `pg_query()` in that situation is `NULL` or `false`.

---

<div class="post-metadata">

**Author:** ![ywy9876](https://avatars.discourse-cdn.com/v4/letter/y/48db29/32.png) [@ywy9876](https://discourse.slimframework.com/u/ywy9876)\
**Post date:** [February 27, 2017, 8:56am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/3 "2017-02-27T08:56:38Z")

</div>

Hello,  
yes, for testing a failure I changed the table name of the query to a non exist table! But the qiestion is, it will enter the condition if (!$result), but with the codes inside it, I mean, set the body and status code, it seems like it’s not returning anything. I checked it with Firefox, but the status code is 204 no content and I can’t see the message I wish…  
Thank you.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 27, 2017, 9:11am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/4 "2017-02-27T09:11:31Z")

</div>

If the status is `204`, then it isn’t entering this code:

```auto
    if (!$result) {
        $data = array("Error Message" => 'Query did not execute successfully');
        $newResponse = $response->withJson($data, 500);
    }

```

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 27, 2017, 9:14am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/5 "2017-02-27T09:14:00Z")

</div>

Oh, `204` is this code:

```auto
        if (pg_num_rows($result) == 0) {
            $data = array("Warning Message" => "There's no existent Model!");
            $newResponse = $response->withJson($data, 204);

        }

```

You won’t get a message with `204`. From the [spec](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5):

> The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

For an error response you should use a status code greater than or equal to `400`.

---

<div class="post-metadata">

**Author:** ![ywy9876](https://avatars.discourse-cdn.com/v4/letter/y/48db29/32.png) [@ywy9876](https://discourse.slimframework.com/u/ywy9876)\
**Post date:** [February 27, 2017, 10:06am UTC](https://discourse.slimframework.com/t/how-to-return-response-with-custom-status-code-and-message-as-json-for-response-body-with-slim-framework/1202/6 "2017-02-27T10:06:34Z")

</div>

Hi,  
then I don’t understand why it does not enter the condotion if (!$result) { } if the query failed. The interesting thing is if I put echo ‘some error’ inside this condition and delete the return $newResponse, I can see it with Firefox…
