【问题标题】:Default headers not applying to request默认标头不适用于请求
【发布时间】:2017-03-06 10:06:27
【问题描述】:

新的暴饮暴食。我正在尝试使用它联系 REST 端点。通过 curl 发送请求或使用类似 postman app for chrome 的东西返回预期的 JSON 响应。使用下面的 guzzle 发送它会返回一个 404 错误,类似于我在没有包含标题的情况下点击 URL 时返回的错误。

为什么标头没有进入此请求?

 // Get extra detail for the object
 $client = new \GuzzleHttp\Client([
   'base_uri' => env('OPENIDM_URL'),
   'headers' => [
     'Content-Type' => 'application/json',
     'X-OpenIDM-Username' => env('OPENIDM_USER'),
     'X-OpenIDM-Password' => env('OPENIDM_PASS'),
     'Authorization' => 'Basic Og=='
   ]
 ]);

 $request = new \GuzzleHttp\Psr7\Request('GET', $attributes['sourceobjectid']);
 $res = $client->send($request);

我已经转储了客户端和请求对象的内容。它们如下所示:

Client {#181 ▼
  -config: array:8 [▼
    "base_uri" => Uri {#188 ▼
      -scheme: "https"
      -userInfo: ""
      -host: "my.url.here.com"
      -port: null
      -path: "/openidm"
      -query: ""
      -fragment: ""
    }
    "headers" => array:5 [▼
      "Content-Type" => "application/json"
      "X-OpenIDM-Username" => "myuser"
      "X-OpenIDM-Password" => "mypass"
      "Authorization" => "Basic Og=="
      "User-Agent" => "GuzzleHttp/6.2.1 curl/7.38.0 PHP/5.6.26-0+deb8u1"
    ]
    "handler" => HandlerStack {#169 ▶}
    "allow_redirects" => array:5 [▶]
    "http_errors" => true
    "decode_content" => true
    "verify" => true
    "cookies" => false
  ]
}
Request {#189 ▼
  -method: "GET"
  -requestTarget: null
  -uri: Uri {#190 ▼
    -scheme: ""
    -userInfo: ""
    -host: ""
    -port: null
    -path: "managed/user/eb758aab-7896-4196-8989-ba7f97a7e962"
    -query: ""
    -fragment: ""
  }
  -headers: []
  -headerNames: []
  -protocol: "1.1"
  -stream: null

任何建议将不胜感激。

【问题讨论】:

标签: guzzle guzzle6


【解决方案1】:

如果您自己构造请求对象,Guzzle 不会对其应用配置。

您要么必须使用从客户端调用的便捷 HTTP 方法(get、put 等),要么使用自定义中间件。

第一个更容易,第二个给你更多的权力,但也有责任。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2012-03-17
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多