使用 Laravel 请求对象设置发布数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27293066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:30:42  来源:igfitidea点击:

Setting post data with a Laravel request object

phplaravel

提问by Andy

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

我正在尝试测试 Laravel API 端点并想在代码中调用它。

    $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.

此代码段适用于 GET,但我也需要能够设置 POST 调用。将 $method 设置为 POST 也有效,但我找不到详细说明如何附加发布数据的文档。

Any advice?

有什么建议吗?

回答by lukasgeiter

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 $parametersas third argument:

正如您在评论中提到的,您可以使用,$this->call()但实际上您也可以使用当前代码来实现。如果您查看Request::create()函数的签名,您可以看到它$parameters作为第三个参数:

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

并且文档块说: The query (GET) or request (POST) parameters

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

所以你可以简单地将数据添加到 Request::create()

$data = array('foo' => 'bar');
$request = Request::create( $path, $method, $data );
$response = Route::dispatch( $request );

回答by Leonel Elimpe

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

我花了将近一天的时间试图让自己使用护照和 Angular 前端进行社会认证。

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

当我使用 Restlet API Client 发出请求时,我总是得到成功的响应。 Restlet 客户端请求

Restlet client response

Restlet 客户端响应

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.

我觉得这与请求中发送表单数据的方式有关,因此在寻找在 laravel 中发出此类内部请求的正确方法时,我遇到了这个具有工作实现的示例项目:passport-social-grant -例子

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.

希望这可以帮助。