skip to Main Content

This is the error message I am getting, when hitting the url http://localhost:8000/test
cURL error 6: Could not resolve host: api (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for api/test

using full url Like this Http::get(‘http://localhost:8000/api/test’)

get this error

cURL error 28: Connection timeout after x as well

Here is the api file for defining route for /api/test

api.php

Route::get('/test', function(){
    return response()->json([
        'name' => 'John Doe',
        'address' => 'USA'
    ]);
});

web.php

Route::get('/test', [WebPostController::class, 'info']);

WebPostController.php

class WebPostController extends Controller
{

    public function info()
    {
        $response = Http::get('/api/test');

        $data = $response->json();
        return $data;
    }

P.S: External api is working fine however internal api which has created inside the same laravel app is getting failed . The api endpoint is working fine in the POSTMAN.

Version – PHP 8.2.12 — Laravel 11.27.2

I want to fetch the data from api endpoint like this

{
  "name": "X",
  "address": "Y",
}

2

Answers


  1. Chosen as BEST ANSWER

    First of all the issue was coudn't resolved host which wasn't primary issue though. But after getting comments from this post i realized why that happened. Then the primaty issue i got which was server getting me timed out after certain time.

    Well, the issue is the default server of php i am using for laravel which can handle only single request at a time so when i was making request through url it coudn't handle multiple request at a time (browser request + api request) so i have to switched apache server which solved the problem of the time out.

    #thanks


  2. If you change

    Http::get('/api/test')
    

    to

    Http::get('http://localhost:8000/api/test')
    

    Then it might work.. This is because the URL you are passing is being "read" as http://api/test and the hostname/domain api doesn’t resolve to anything.

    However, if you are wanting to access an endpoint that exists within your application you can skip the whole HTTP setup process and directly access the method (Access Controller method from another controller in Laravel 5):

    app('AppHttpControllersApiPostController')->getTest();
    

    But ultimately, I would recommend moving code that you are going to reuse into a shared library.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search