laravel - guzzle 发布请求返回格式错误的错误
问题描述
我尝试运行护照'/oauth/clients'方法来创建新护照的客户并得到
local.ERROR: cURL error 3: <url> malformed error
我愿意 :
$app_root_url = config('app.url');
\Log::info('-1 REGISTER $app_root_url::');
\Log::info($app_root_url);
$headers = [
'content-type' => 'application/json',
'verify' => true,
];
$guzzleClient = new \GuzzleHttp\Client();
$url = '/oauth/clients';
$requestBody['name'] = $newUser->name;
$requestBody['redirect'] = $app_root_url . '/callback';
// $requestBody['Accept'] = 'application/json'; // Do I need this parameter ?
\Log::info('-2 REGISTER $requestBody::');
\Log::info($requestBody);
$request = $guzzleClient->post( $url, [
"headers" => $headers, // Do I need these params ?
'form_params' => $requestBody // ERROR REFERING THIS LINE
]);
$response = $request->send();
\Log::info('-3 REGISTER $response::');
\Log::info($response);
我记录我看到的文件:
local.INFO: -1 REGISTER $app_root_url::
local.INFO: http://local-hostels3-backend-api.com
local.INFO: -2 REGISTER $requestBody::
local.INFO: array (
'name' => 'admin',
'redirect' => 'http://local-hostels3-backend-api.com/callback',
)
local.ERROR: cURL error 3: <url> malformed (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) {"exception":"[object] (GuzzleHttp\\Exception\\RequestException(code: 0): cURL error 3: <url> malformed (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) at /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:201)
[stacktrace]
#0 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(155): GuzzleHttp\\Handler\\CurlFactory::createRejection(Object(GuzzleHttp\\Handler\\EasyHandle), Array)
#1 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(105): GuzzleHttp\\Handler\\CurlFactory::finishError(Object(GuzzleHttp\\Handler\\CurlHandler), Object(GuzzleHttp\\Handler\\EasyHandle), Object(GuzzleHttp\\Handler\\CurlFactory))
#2 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\\Handler\\CurlFactory::finish(Object(GuzzleHttp\\Handler\\CurlHandler), Object(GuzzleHttp\\Handler\\EasyHandle), Object(GuzzleHttp\\Handler\\CurlFactory))
#3 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(28): GuzzleHttp\\Handler\\CurlHandler->__invoke(Object(GuzzleHttp\\Psr7\\Request), Array)
#4 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(51): GuzzleHttp\\Handler\\Proxy::GuzzleHttp\\Handler\\{closure}(Object(GuzzleHttp\\Psr7\\Request), Array)
#5 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(66): GuzzleHttp\\Handler\\Proxy::GuzzleHttp\\Handler\\{closure}(Object(GuzzleHttp\\Psr7\\Request), Array)
#6 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Middleware.php(29): GuzzleHttp\\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\\Psr7\\Request), Array)
#7 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(70): GuzzleHttp\\Middleware::GuzzleHttp\\{closure}(Object(GuzzleHttp\\Psr7\\Request), Array)
#8 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Middleware.php(59): GuzzleHttp\\RedirectMiddleware->__invoke(Object(GuzzleHttp\\Psr7\\Request), Array)
#9 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/HandlerStack.php(71): GuzzleHttp\\Middleware::GuzzleHttp\\{closure}(Object(GuzzleHttp\\Psr7\\Request), Array)
#10 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Client.php(361): GuzzleHttp\\HandlerStack->__invoke(Object(GuzzleHttp\\Psr7\\Request), Array)
#11 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Client.php(163): GuzzleHttp\\Client->transfer(Object(GuzzleHttp\\Psr7\\Request), Array)
#12 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Client.php(183): GuzzleHttp\\Client->requestAsync('post', Object(GuzzleHttp\\Psr7\\Uri), Array)
#13 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/vendor/guzzlehttp/guzzle/src/Client.php(96): GuzzleHttp\\Client->request('post', '/oauth/clients', Array)
#14 /mnt/_work_sdb8/wwwroot/lar/hostels3-backend-api/app/Http/Controllers/AuthController.php(207): GuzzleHttp\\Client->__call('post', Array)
#15 [internal function]: App\\Http\\Controllers\\AuthController->register(Object(Illuminate\\Http\\Request))
在我的 app/Providers/AuthServiceProvider.php 中,我添加了护照路线定义:
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
...
"guzzlehttp/guzzle": "^6.5",
"laravel/framework": "^6.2",
"laravel/passport": "^8.1",
是什么引发了这个错误以及如何解决它?
修改块: 我在标题中添加了参数:
$headers = [
'content-type' => 'application/json',
'verify' => true,
'X-CSRF-TOKEN', csrf_token()
];
但是调试这个数组我看到它包含:
array (
'content-type' => 'application/json',
'verify' => true,
0 => 'X-CSRF-TOKEN',
1 => NULL,
)
那是后端 rest api 应用程序,我没有隐藏 csrf 的任何形式。我必须以某种方式初始化 csrf 吗?
谢谢!
解决方案
您的变量似乎$url
只有路径部分缺少它必须定位的任何主机。
在变量前面加上路由主机,从而为 Guzzle 客户端提供完整的 URL 值。
可能的解决方案:
$url = config('app.url') . '/oauth/clients';
编辑
您还可以将您的设置app URL
为 Guzzle 客户端的基本 URL。
推荐阅读
- flutter - 将流列表添加到块颤动
- c++ - CodeLite 不在某些工作区中运行 C++ 程序
- bash - 使用 read() 或 run() 运行 bash 命令时,我应该如何转义 '&&' 和其他特殊字符?
- javascript - 为什么不能在 JavaScript 函数声明中使用箭头函数表达式语法?
- r - 通过 2 个矩阵 (R) 的线性回归求斜率
- pandoc - Pandoc 可以从 vimwiki 源输出语法突出显示的代码块吗?
- python - 为什么在 GCP 上从 python 生成重复的 UUID?
- r - 迭代R中的公式元素
- python - 如果 abs(x)>T 实现 x=T 作为 pytorch 中的激活函数
- python - 当我用“?”分割字符串时,为什么会创建一个空字符串,并且只有一个字符串?