DEV Community

Laravel Lions
Laravel Lions

Posted on

How to call Laravel internal API ?

Today I am sharing my experience of how I spend a day consuming an internal API within the same laravel application and ended up with this post ….

Are you ready to get your query solved ?? Let's get started …… :)
Query: How can we make an internal API call within the same laravel Api application from the Controller?

Answer: Ok let's go through the post to get answered.

Note :: We can use http-clients called Guzzle HTTP client for making an external api call in laravel app or we can go with axios (https://github.com/axios/axios).

See more about laravel http request at :
https://laravel.com/docs/8.x/http-client
(https://github.com/axios/axios).

If you have an API, which you also use on your site..then you might want to get the data from the API by making an internal request.

Don’t forget to include it before making the request.
use Illuminate\Support\Facades\Route;

image

In this way,image

you can make an internal API call in laravel app .
WAIT WAIT its not over !!!!!!!!!(https://github.com/axios/axios).
What if you have to pass some data in the header(like authorization token) with the call ???

Then you can include the headers as :
image

OK By now you might have got your data returned by api ..
Bonus Section
You can get your data as defined in your response sent by the api call ….
In my case I got my data and passed it to the view as below :

image

So the final code looks as:
image

Thank You…
If You found the content useful. DON’T forget to give applause and let me know if your problem is solved in the comment section :)
See You Soon in the next post …. SO keep Following …. :)
Happy Coding …. :)

Hire Laravel Development Company

Top comments (0)