0

I'm using dingo/api laravel package to create an API.

I want to add success key response to every API call. If we have an error, success: false and if all is OK, then success: true.

Final response must looks like this:

{
"success":true,
"data": [{}, {}]
}

I know that there is a ->setMeta($meta); method to add extra keys like this :

$meta = array(
    'success' => 'true',
    'status_code' => 200
);

return $this->response->collection($users, new UserTransformer)->setMeta($meta);

that creates bellow response :

{
    "data": [{}, {}],
    "meta": {
        "success": true,
        "status_code": 200
    },
}

As you can see ,setMeta adds extra keys under a meta key that I do not want that.

I readed it's Responses documentations but I could not find any solution.

How to do that?

Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159
  • Why `meta` is not an option for you ? – Wrong Oct 17 '16 at 15:38
  • @Wrong,To simplify More. in most of major API that I checked there is comes a simple key like `ok` or `success` with a boolean value that shown result status In short. in the other hand, front-end developer has a simpler task too. – Ahmad Badpey Oct 18 '16 at 05:11
  • 1
    a REST API is based on http codes for success/error reporting. If your developer gets an HTTP 200 OK status, it means that request was successful. For other types of error/success reporting of business logic, you can use the meta key in your dingo api response. Or you can create the json response manually (you can even use transformers in a foreach loop) and build any type of response structure you need using `$this->response->array()` method – Wrong Oct 31 '16 at 14:38

2 Answers2

1

I know it was pain. After few hours of tries. this is what i found. Dingo api have no consistent API response by default. For all success results it will just return data and meta, but for error it will return status code and message (this is because all error response is use Exception handler).

For success response you need to use this way https://github.com/dingo/api/wiki/Responses#morphing-and-morphed-events

this is event listener and every response send out, it will morph the response first. so that you can have more control on how response looks like. e.g

create a listener file under app/Listeners directory.

use Dingo\Api\Event\ResponseWasMorphed;

class AddSuccessKeyToResponse
{
    public function handle(ResponseWasMorphed $event)
    {
        $event->content['success'] = 1;

    }
}

Add listen event in app/Providers/EventServiceProvider.php

protected $listen = [
    'Dingo\Api\Event\ResponseWasMorphed' => [
        'App\Listeners\AddSuccessKeyToResponse'
    ]
];

For error response you just need to edit the config/api.php file errorFormat, so that all Exception handler error response will according to the format. e.g

'errorFormat' => [
  'success' => false,
  'message' => ':message',
  'errors' => ':errors',
  'code' => ':code',
  'status_code' => ':status_code',
  'debug' => ':debug',
]
Edwin Wong
  • 703
  • 1
  • 8
  • 11
0

This will help you

$api->get('book/{id}', function ($id){
    $book = Book::FindOrFail($id);
    $_res['success'] = true;
    $_res['book'] = $book;
    return $_res;
});

and edit config/api.php

'errorFormat' => [
    'success' => false,
    'message' => ':message',
    'errors' => ':errors',
    'code' => ':code',
    'status_code' => ':status_code',
    'debug' => ':debug',
],
Thanvandh
  • 76
  • 2
  • 5