0

I'm trying to use GuzzleHttp to make a POST request from projectA to projectB. Both of the projects run on the same Homestead instance. Problem is the endpoint doesn't get the request.

The Windows hosts file:

192.168.10.10 projectA.local
192.168.10.10 projectB.local

The GuzzleHttp call in projectA:

$this->httpClient = new Client(array(
    'base_uri' => 'http://projectB.local/api/'
));

$response = $this->httpClient->post('create-user', array('form_params' => $this->params));

return $response->getStatusCode();

Debug in projectB:

public function createUser(Request $request) {
    Logger::debug('API call received'); // custom logging of the call in the database
}

Now, this API call is never logged in the DB. projectA sends the request, but projectB doesn't receive it.

What I confirmed:

  • The $this->params is set and correct (these are the POST params)
  • The endpoint URI is correct (http://projectB.local/api/create-user)
  • The endpoint exists in projectB, is accessible and responds to POST
  • The response status code is 200

What am I missing? Is it not possible to call one Homestead project from another one?


UPDATE - configuration from Homestead

/etc/hosts

127.0.0.1       homestead       homestead
127.0.0.1       localhost
127.0.1.1       vagrant.vm      vagrant
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

/etc/resolv.conf

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.0.2.3

/etc/nsswitch.conf

> grep hosts /etc/nsswitch.conf
> hosts:          files dns

host check

> host projectB.local
> Host projectB.local not found: 4(NOTIMP)
> Host projectB.local not found: 4(NOTIMP)
lesssugar
  • 15,486
  • 18
  • 65
  • 115

1 Answers1

0

OK, I started thinking that it might be something breaking the API call before the endpoint is reached. I have some global middlewares running on every request (e.g. the VerifyCsrfToken one).

I made sure my endpoint path is excluded from processing in the middlewares and that was it:

if (!$request->is('api/*)) {
    // handle...
}

Specifically, in the VerifyCsrfToken middleware, you can use the provided field:

protected $except = [
    'api/*'
];

Sounds so obvious now it's almost disgusting.

lesssugar
  • 15,486
  • 18
  • 65
  • 115