为什么GuzzleHttp客户端在使用它在Laravel / Lumen上发出网络请求时会抛出ClientException?

I am currently building a Financial micro service application using Laravel/Lumen micro framework.Everything have been working perfectly as expected. My problem now is that i am trying to make a network request to my internal services via Api call from ApiGateway using GuzzleHttp client. The problem is that when i make request to the internal service, it always throws an exception of ClientException.

ClientException.

Client error: GET http://127.0.0.1:8081/v1/admin resulted in a 401 Unauthorized response: {"error":"Unauthorized.","code":401}

I have tried to make network request to the same internal services using postman; and it works fine. However, for some reason still fail to work with GuzzleHttp. I don't know what i am doing wrong. Please your assist will be appreciated.

Here is the httpClient.php in ApiGateway.

//Constructor method
public function __construct() {
    $this->baseUri = config('services.auth_admin.base_uri');
}

public function httpRequest($method, $requestUrl, $formParams = [], $headers = []) {
    //Instantiate the GazzleHttp Client
    $client = new Client([
        'base_uri' => $this->baseUri,
    ]);
    //Send the request
    $response = $client->request($method, $requestUrl, ['form_params' => $formParams, 'headers' => $headers]);
    //Return a response
    return $response->getBody();
}

//Internal Service Communication in ApiGateway** 
public function getAdmin($header) {
    return $this->httpRequest('GET', 'admin', $header);
}

InternalServiceController.php

   public function getAdmin(Request $request) {
        return $this->successResponse($this->authAdminService->getAdmin($request->header()));
    }

I am using Lumen version: 5.8 and GuzzleHttp Version: 6.3

You pass your headers as formParams (third index instead of fourth).

Try below:

return $this->httpRequest('GET', 'admin', [], $header);

I am making some assumptions here which I hope should be helpful to you.

PHP does not support skipping optional parameters and thus you should pass an empty array [] when calling httpRequest().

public function httpRequest($method, $requestUrl, $formParams = [], $headers = [], $type='json', $verify = false) {
    //Instantiate the GazzleHttp Client
    $client = new Client([
        'base_uri' => $this->baseUri,
    ]);

    //the request payload to be sent
    $payload = [];

    if (!$verify) {
       $payload['verify'] = $verify; //basically for SSL and TLS
    }

    //add the body to the specified payload type
    $payload[$type] = $formParams;

    //check if any headers have been passed and add it as well
    if(count($headers) > 0) {
        $payload['headers'] = $headers;
    }

    //Send the request
    $response = $client->request($method, $requestUrl, $payload);
    //Return a response
    return $response->getBody();
}

Now you need to call it in this manner when you are not passing in any form_params or body

//Internal Service Communication in ApiGateway** 
 public function getAdmin($header) {
     return $this->httpRequest('GET', 'admin', [], $header);
 }