Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force api json #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand All @@ -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",
Expand All @@ -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'
```

Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down
23 changes: 23 additions & 0 deletions app/Http/Middleware/JsonResponseMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class JsonResponseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');

return $next($request);
}
}