【问题标题】:Same request on cURL and GuzzleHTTP. Different responsescURL 和 GuzzleHTTP 上的相同请求。不同的反应
【发布时间】:2021-02-15 08:47:47
【问题描述】:

我在我的 API 中使用 GuzzleHTTP。我正在使用 Lumen 开发用于与 HashiCorp Vault API 通信的 API。为此,我安装了 GuzzleHTTP 来进行 API 调用。

例如,我正在尝试根据特定角色为数据库用户生成凭据。在命令行 cURL 请求向我响应新用户的凭据。虽然使用 Guzzle 并没有给我任何与新创建用户相关的信息。

我是 Guzzle 的新手,所以我很乐意提供任何建议。

cURL 请求如下所示:

curl -X GET \
    --header "X-Vault-Token: $VAULT_TOKEN" \
    http://192.168.1.3:8200/v1/database/creds/my-role | json_pp

响应给了我我想要的凭据:

{
   "request_id" : "3eacc89b57b-2e2e-af6f-849f-868bdbfd2dc5e1",
   "wrap_info" : null,
   "renewable" : true,
   "data" : {
      "username" : "randomgeneratedusername",
      "password" : "randomgeneratedpassword"
   },
   "auth" : null,
   "lease_duration" : 3600,
   "warnings" : null,
   "lease_id" : "database/creds/my-role/randomsecretname"
}

当我的 Guzzle 代码如下所示时:

class MySQLVaultAPI extends VaultAPI
{
    public function createUser() {
        $response = $this->getClient()->request('GET', $this->url.'/database/creds/my-role',
            ['headers' => $this->headers]);

    }
}

还有:

class VaultAPI
{
    protected string $url;
    protected Client $http;
    protected array $headers;

    public function __construct(Client $client)
    {
        $this->url = 'http://192.168.1.3:8200/v1';
        $this->http = $client;
        $this->headers = [
            'X-Vault-Token' => 'secrettoken',
            'Accept' => 'application/json',
        ];
    }
}

以及来自 GuzzleHTTP 的响应(我在 $response 对象上使用了 symfony 转储程序):

^ GuzzleHttp\Psr7\Response {#43 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:4 [▼
    "Cache-Control" => array:1 [▶]
    "Content-Type" => array:1 [▶]
    "Date" => array:1 [▶]
    "Content-Length" => array:1 [▶]
  ]
  -headerNames: array:4 [▼
    "cache-control" => "Cache-Control"
    "content-type" => "Content-Type"
    "date" => "Date"
    "content-length" => "Content-Length"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream {#21 ▼
    -stream: stream resource @137 ▶}
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}

【问题讨论】:

    标签: php api curl lumen guzzle


    【解决方案1】:

    好吧,

    我已经找到答案了。所有内容都在GuzzleHttp\Psr7\Stream

    所以我不得不在我的VaultAPI 服务中添加额外的方法

    public function getFullBody(Stream $responseBody)
    {
        $responseBody->seek(0);
        $output = json_decode(trim($responseBody->getContents()), true);
        if ($output === null) {
            throw new \Exception('Error parsing response JSON ('.json_last_error().')');
        }
    
        return $output;
    }
    

    然后在我的MySQLVaultAPI 里面我叫它

    public function createUser() {
        $response = $this->getClient()->request('GET', $this->url.'/database/creds/my-role',
            ['headers' => $this->headers]);
        return $this->getFullBody($response->getBody());
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2019-12-09
      • 2020-07-07
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 2023-03-03
      相关资源
      最近更新 更多