Skip to content
Advertisement

Laravel Http Client add api key into request

On my Laravel project (laravel version 9) I have to connect on a third part api using an apikey on the url and I have to get the response in json format.

To not having to repeat the same code over and over and over I’m trying to use the Http Client Macro for setting the base url, make sure I always get the response in JSON and add always the apikey to the url.

This api needs to get the apikey on the url like a query parameter something like this:

This is my macro function so far (this is at the AppServiceProvider.php):

JavaScript

I have tried adding an options array at the end like this:

JavaScript

But this seems to not work as it’s not adding the api key to the url.

What I’m trying to do its that when I call the interceptor like this:

JavaScript

The macro should add the api to the url key like this:

JavaScript

For now it’s calling the correct url and I’m getting the response in JSON format but it’s not adding the apikey to the url.

I’ve checked the docs and couldn’t found any function to do this. Is there other function to achieve this?

Advertisement

Answer

I finnally found a solution.

On my macro I combine the query parameters of my request with a second one that contains the api key:

JavaScript

And then on my controller I just run it with my query parameters:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement