postman 设置api_token,测试数据等操作

Posted strenghthen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postman 设置api_token,测试数据等操作相关的知识,希望对你有一定的参考价值。

在postman的环境变量中先设置好一个用户的token(事先你的数据库里面要有用户的这个数据),如下图:

然后在api请求的url 的head头部添加对应的token键名,value值用花括号{{token}}包住,就会自动获取其值。

 

1、routes.php文件设置中间件路由,将你的控制器放在里面

    Route::group([\'middleware\' => \'auth.api\'], function () {

        Route::controllers([

            \'check_lists\' => \'CheckListsController\',
        ]);
    });

 

中间件middleware文件目录下创建AuthenticateApi.php

<?php

namespace App\\Http\\Middleware;

use Closure;
use App\\Models\\User;
use Illuminate\\Support\\Facades\\Auth;

class AuthenticateApi
{
    public function handle($request, Closure $next)
    {

        $token = $request->header(\'Token\');
        if (! $token) {
            return \\JSend::error(\'Missing Api Token.\', 401);
        }

        $user = User::where(\'token\', $token)->first();
        if (! $user) {
            return \\JSend::error(\'Invalid Api Token.\', 401);
        }

        Auth::setUser($user);

        return $next($request);
    }
}

在Kernel.php文件里面注册该中间件。

<?php

namespace App\\Http;

use Illuminate\\Foundation\\Http\\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application\'s global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [

    ];

    /**
     * The application\'s route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        \'web\' => [
            \\App\\Http\\Middleware\\EncryptCookies::class,
            \\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,
            \\Illuminate\\Session\\Middleware\\StartSession::class,
            \\Illuminate\\View\\Middleware\\ShareErrorsFromSession::class,
            \\App\\Http\\Middleware\\VerifyCsrfToken::class,
        ],

        \'api\' => [
            // \'throttle:60,1\',
        ],
    ];

    /**
     * The application\'s route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
    
        \'auth.api\'   => \\App\\Http\\Middleware\\AuthenticateApi::class,
    
        \'auth.basic\' => \\Illuminate\\Auth\\Middleware\\AuthenticateWithBasicAuth::class,
        \'can\' => \\Illuminate\\Foundation\\Http\\Middleware\\Authorize::class,
        \'guest\' => \\App\\Http\\Middleware\\RedirectIfAuthenticated::class,
        \'throttle\' => \\Illuminate\\Routing\\Middleware\\ThrottleRequests::class,
    ];
}

接着,在url请求的控制器里面验证下时候请求成功

<?php

namespace App\\Http\\Controllers\\Api;

use App\\Models\\CheckList;
use App\\Models\\User;
use App\\Models\\Good;
use App\\Models\\GoodCate;
use Illuminate\\Http\\Request;

use App\\Http\\Controllers\\Controller;

class CheckListsController extends Controller
{

    //添加检查清单
    public function postAddList(Request $request)
    {
        dd(auth()->user()->id);

成功获取到该token对应的用户的ID;

 

以上是关于postman 设置api_token,测试数据等操作的主要内容,如果未能解决你的问题,请参考以下文章

postman

Postman 接口测试神器

postman

使用Postman发送请求(超详细)

postman参数化

postman使用之四:设置读取变量和切换环境