guzzle post请求返回格式错误

huangapple go评论106阅读模式
英文:

guzzle post requests returns malformed error

问题

I try to run passport '/oauth/clients' method to create a new passport's client with Guzzle and got:

local.ERROR: cURL error 3: '' malformed error

I do:

$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';

\Log::info('-2 REGISTER $requestBody::');
\Log::info($requestBody);

$request = $guzzleClient->post($url,  [
    "headers" => $headers,
    'form_params' => $requestBody // ERROR REFERING THIS LINE
]);
$response = $request->send();
\Log::info('-3 REGISTER $response::');
\Log::info($response);

I log file I see:

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: '' malformed (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) {"exception":"[object] (GuzzleHttp\Exception\RequestException(code: 0): cURL error 3: '' 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->__call('post', 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))


In my app/Providers/AuthServiceProvider.php, I added passport routes definition:

```php
public function boot()
{
    $this->registerPolicies();
    Passport::routes();
}

...

"guzzlehttp/guzzle": "^6.5",
"laravel/framework": "^6.2",
"laravel/passport": "^8.1"

What raised this error, and how to fix it?

MODIFIED BLOCK:
I added a parameter to my headers:

$headers =  [
    'content-type' => 'application/json',
    'verify' => true,
    'X-CSRF-TOKEN' => csrf_token()
];

But debugging this array, I see it contains:

array (
  'content-type' => 'application/json',
  'verify' => true,
  0 => 'X-CSRF-TOKEN',
  1 => NULL,
)

That is a backend REST API app, and I do not have any form with CSRF hidden in it. Do I have to initialize CSRF in some way?

Thanks!

英文:

I try to run passport '/oauth/clients' method to create new passport's client with guzzle and got

local.ERROR: cURL error 3: <url> malformed error 

I do :

            $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);

I log file I see :

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))

In my app/Providers/AuthServiceProvider.php I added passport routes definition :

public function boot()
{
    $this->registerPolicies();
    Passport::routes();
}

...

 "guzzlehttp/guzzle": "^6.5",
 "laravel/framework": "^6.2",
 "laravel/passport": "^8.1",

What raized this error and how to fix it ?

MODIFIED BLOCK:
I added parameter to my headers :

$headers =  [
    'content-type' => 'application/json',
    'verify' => true,
    'X-CSRF-TOKEN', csrf_token()
];

But debugging this array I see it containes :

array (
  'content-type' => 'application/json',
  'verify' => true,
  0 => 'X-CSRF-TOKEN',
  1 => NULL,
)  

That is backend rest api app and I do not any form with csrf hidden in it.
Have I to init csrf in someway?

Thanks!

答案1

得分: 1

似乎您的$url变量只包含路径部分,缺少必须定位的主机。

在变量前加上路由主机,从而为Guzzle客户端提供完整的URL值。

可能的解决方案:

$url = config('app.url') . '/oauth/clients';

编辑
您还可以将您的app URL设置为Guzzle客户端的基本URL。

英文:

It seems like your $url variable is only having path sections missing any host it must target.

Prepend the variable with the route host and thus providing full URL value for the Guzzle client.

Probable solution:

$url = config('app.url') . '/oauth/clients';

EDIT
You can also set your app URL as the base URL for Guzzle client.

huangapple
  • 本文由 发表于 2020年1月6日 15:19:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定