From dbcb1643b5d76508cb4fa23fc22a9cfb526d2fdd Mon Sep 17 00:00:00 2001 From: Guilherme Costa Date: Sat, 1 Apr 2023 14:52:18 -0300 Subject: [PATCH 1/3] feat: add middleware that forces json in api --- .../Middleware/JsonResponseMiddleware.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/Http/Middleware/JsonResponseMiddleware.php diff --git a/app/Http/Middleware/JsonResponseMiddleware.php b/app/Http/Middleware/JsonResponseMiddleware.php new file mode 100644 index 0000000..8e84604 --- /dev/null +++ b/app/Http/Middleware/JsonResponseMiddleware.php @@ -0,0 +1,23 @@ +headers->set('Accept', 'application/json'); + + return $next($request); + } +} From 2007f8e2520db495aea3060068a30f031f2c024c Mon Sep 17 00:00:00 2001 From: Guilherme Costa Date: Sat, 1 Apr 2023 16:16:32 -0300 Subject: [PATCH 2/3] feat: call json response middleware in kernel --- app/Http/Kernel.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 88660d3..6fc113a 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -20,6 +20,7 @@ class Kernel extends HttpKernel { \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, + \App\Http\Middleware\JsonResponseMiddleware::class, ]; /** From 023a8973cf64b729f5b1966c110ee6b321a6eeac Mon Sep 17 00:00:00 2001 From: Guilherme Costa Date: Sat, 1 Apr 2023 16:20:21 -0300 Subject: [PATCH 3/3] adjust: remove alert for accept header of doc --- README.md | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/README.md b/README.md index 9a60135..96060a3 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ Hydra is a zero-config API boilerplate with Laravel Sanctum and comes with excel - [Default Admin Username and Password](#default-admin-username-and-password) - [Default Role for New Users](#default-role-for-new-users) - [Single Session or Multiple Session](#single-session-or-multiple-session) - - [Add `Accept: application/json` Header In Your API Calls (Important)](#add-accept-applicationjson-header-in-your-api-calls-important) - [Logging](#logging) - [Code Formatting](#code-formatting) - [Tutorial](#tutorial) @@ -789,24 +788,6 @@ Hydra doesn't invalidate the previously issued access tokens when a user authent This ENV variable is configured in in `config/hydra.php`, and then used in `app/Http/Controllers/UserController.php` -### Add `Accept: application/json` Header In Your API Calls (Important) - -This is very important. To properly receive JSON responses, add the following header to your API requests. - -```shell -Accept: application/json -``` - -For example, if you are using `curl` you can make a call like this. - -```shell -curl --request GET \ - --url http://localhost:8000/hydra/version \ - --header 'Accept: application/json' \ - --header 'Content-Type: application/x-www-form-urlencoded' \ - --data = -``` - ### Logging Hydra comes with an excellent logger to log request headers, parameters and response to help debugging and inspecting API calls. All you have to do is wrap the route with 'hydra.log' middleware, as shown below @@ -901,7 +882,6 @@ To create a new user, you can place a curl request or use tools like Postman, In ```shell curl --request POST \ --url http://localhost:8000/api/users \ - --header 'Accept: application/json' \ --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \ --form 'name=Hydra User' \ --form email=user@hydra.project \ @@ -913,7 +893,6 @@ Great! Now we have our users. Let's login as this new user using curl (You can u ```shell curl --request POST \ --url http://localhost:8000/api/login \ - --header 'Accept: aplication/json' \ --header 'Content-Type: application/json' \ --data '{ "email": "user@hydra.project", @@ -934,7 +913,6 @@ Now let's test our protected route. Add this bearer token in your PostMan/Insomn ```shell curl --request GET \ --url http://localhost:8000/api/greet \ - --header 'Accept: application/json' \ --header 'Authorization: Bearer 5|gbiWdd7yJFYiTIgoK1jK3C7HZJtJUK1PnBIToBLN' ```