I'm trying to use my own api internally but still need to set headers. Were you able to figure out a way to do this?
No, we didn't work out a way to do this.
We ended up building the app that would consume the API completely separately from the API itself as we considered this better separation of Client app and API.
You should try the Dingo\Api package, it does exactly what you need.
You could use named routes and create a class that accepts a route name and parameters. It would return the route instance using getByName(). You can then create a request using route instances' uri and method along with your json parameters. You could then use a filter or milddleware to check each request for json and if so, decode and replace the request. If the response is json, it will be returned as json. If not, Response::json($response).
You probably figured this out already, but this works for me. (didn't test payload, but headers work fine)
$payload = '{ "username": "John" }';
$request = Request::create('/api/users', 'GET', [], [], [], [], $payload);
$headers = Route::getCurrentRequest()->headers();
$headers->set('Content-Type', 'application/json');
$headers->set('X-Token', 'FooBarBazBat');
$response = Route::dispatch($request);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community