dpbrrczhlwbv849228 2019-05-30 01:24
浏览 399

为什么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

  • 写回答

2条回答 默认 最新

  • dongyan3018 2019-05-30 07:14
    关注

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

    Try below:

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建