Support the ongoing development of Laravel.io →
Requests Input
Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

You should try the Dingo\Api package, it does exactly what you need.

Last updated 1 year ago.
0

Did anyone figure this out? Thanks

Last updated 1 year ago.
0

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).

Last updated 1 year ago.
0

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);
0

Sign in to participate in this thread!

Eventy

Your banner here too?

oskarsmith oskarsmith Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.