Query on returning JSON

41 views
Skip to first unread message

Kumar

unread,
Jan 18, 2018, 8:25:12 AM1/18/18
to ngrest
Hi,

My rest api returning status structure on success/failure scenarios

like,
{
    "status": {
        "status": "success",
        "reason": null
    }
}

My query is, I don't have reason to give in success scenarios. so is there is option to get rid of "reason" member while returning in success scenarios?

Thanks.

Dmitry Utkin

unread,
Jan 19, 2018, 4:34:39 PM1/19/18
to ngrest
I'm afraid, it's not possible.

четверг, 18 января 2018 г., 16:25:12 UTC+3 пользователь Kumar написал:

Kumar

unread,
Feb 2, 2018, 12:38:08 AM2/2/18
to ngrest
I have another query, one my rest service has to return different result structure on success and failure scenario.

Eg: getEmpDetails();

I want this api to return empdetail structure in success case, in failure case this api to return statusMessage structure.

I have no clue for this use case. please help on this scenario.

Dmitry Utkin

unread,
Feb 8, 2018, 3:00:36 AM2/8/18
to ngrest
It's not possible, also it's not standard and will be hard to parse on client side (if you're using some language with static typing like java).

I think it's much clear to just throw an HttpException in case of error, then you can set both of HTTP Status Code and string error.

Example:


    YourStruct YourService::get(int id)
    {
        auto it = dataStorage.find(id);
        NGREST_ASSERT_HTTP(it != storage.end(), HTTP_STATUS_404_NOT_FOUND, "Item not found");
        return it->second;
    }


пятница, 2 февраля 2018 г., 8:38:08 UTC+3 пользователь Kumar написал:
Reply all
Reply to author
Forward
0 new messages