mstdmstd
mstdmstd

Reputation: 3121

guzzle post requests returns malformed error

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!

Upvotes: 1

Views: 1821

Answers (1)

Pusparaj
Pusparaj

Reputation: 1639

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.

Upvotes: 1

Related Questions