【发布时间】:2018-11-14 10:02:36
【问题描述】:
所以,我只是想将所有请求代理到不同的服务器。
我希望一切都一样,除了我想更改基本 uri。
<?php
namespace App\MicroServices\Example\Clients;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
/**
* Class ExampleClient
* @package App\MicroServices\Example\Clients
*/
class ExampleClient extends Client implements ClientInterface
{
/**
* ExampleClient constructor.
*/
public function __construct()
{
parent::__construct([
'base_uri' => config('example.base.uri'),
'timeout' => config('example.client.timeout'),
]);
}
/**
* @param Request $request
*
* @return string
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function proxyToExampleServer(Request $request)
{
$method = $request->getMethod();
$body = json_decode($request->getContent(), true);
$path = $request->path() . ($request->getQueryString() ? ('?' . $request->getQueryString()) : '');
$headers = [];
foreach ($request->header() as $key => $value)
{
$headers[$key] = $value[0];
}
$headers['Authorization'] = $request->bearerToken();
return $this->request($method, $path, ['headers' => $headers, 'body' => $body])->getBody()->getContents();
}
}
Postman 中的响应是 400 Bad Request,我已尝试更改为 'form_params' 和 'json'
有什么想法吗?
【问题讨论】: