Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting post data with a Laravel request object

Tags:

php

laravel

I'm trying to test a Laravel API endpoint and want to call it in code.

    $request = Request::create( $path, $method );
    $response = Route::dispatch( $request );

This snippet works fine for GET but I need to be able to set up POST calls too. Setting the $method to POST works as well, but I can't find documentation detailing how to attach post data.

Any advice?

like image 725
Andy Avatar asked Dec 04 '14 11:12

Andy


People also ask

What is request -> input () in Laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

How do I create a variable request in Laravel?

You can use replace() : $request = new \Illuminate\Http\Request(); $request->replace(['foo' => 'bar']); dd($request->foo); Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously. Hope this helps!

What is Guzzlehttp in Laravel?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

How do you change a request parameter?

You can't change the request parameters but you can add to the request attributes. You can use setAttribute to store the object then use request dispatcher to send the request (and your new attribute) to the JSP.


2 Answers

As you mentioned in the comments, you could use $this->call() but you can actually do it with your current code too. If you take a look at the signature of the Request::create() function you can see that it takes $parameters as third argument:

public static function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)

And the docblock says: The query (GET) or request (POST) parameters

So you can simply add the data to Request::create()

$data = array('foo' => 'bar');
$request = Request::create( $path, $method, $data );
$response = Route::dispatch( $request );
like image 100
lukasgeiter Avatar answered Nov 11 '22 10:11

lukasgeiter


I've spent nearly a day trying to get this working myself for social authentication with passport and Angular front-end.

When I use the Restlet API Client to make the request I always get a successful response. Restlet Client Request

Restlet client response

However using the following method of making internal requests always gave me an error.

$request = Request::create(
    '/oauth/token',
    'POST',
    [
        'grant_type' => 'social',
        'client_id' => 'your_oauth_client_id',
        'client_secret' => 'your_oauth_client_secret',
        'provider' => 'social_auth_provider', // e.g facebook, google
        'access_token' => 'access_token', // access token issued by specified provider
    ]
);

$response = Route::dispatch($request);
$content = json_decode($response->getContent(), true);
if (! $response->isSuccessful()) {
    return response()->json($content, 401);
}

return response()->json([
    'content' => $content,
    'access_token' => $content['access_token'],
    'refresh_token' => $content['refresh_token'],
    'token_type' => $content['token_type'],
    'expires_at' => Carbon::parse(
        $content['expires_in']
    )->toDateTimeString()
]);

This specific error:

{
    error: "unsupported_grant_type",
    error_description: "The authorization grant type is not supported by the 
    authorization server.",
    hint: "Check that all required parameters have been provided",
    message: "The authorization grant type is not supported by the authorization server."
}

I had the feeling it has to do with the way the form data is sent in the request, so while searching for a proper way to make such internal requests in laravel I came across this sample project with a working implementation: passport-social-grant-example.

In summary here's how to do it:

$proxy = Request::create(
    '/oauth/token',
    'POST',
    [
        'grant_type' => 'social',
        'client_id' => 'your_oauth_client_id',
        'client_secret' => 'your_oauth_client_secret',
        'provider' => 'social_auth_provider', // e.g facebook, google
        'access_token' => 'access_token', // access token issued by specified provider
    ]
);

return app()->handle($proxy);

Hope this helps.

like image 23
Leonel Elimpe Avatar answered Nov 11 '22 10:11

Leonel Elimpe