大摇大摆地尝试重定向到登录

swagger try it out redirect to login

我安装了我的 swagger api 文档(使用登录名来保护我的 "api/documentation" 路径)。文档 UI 没问题,但是当我想执行时 "try it out"... ups!出了点问题...

我发现 class "Authenticate"(扩展中间件)如果找不到 header this.headers['Content-Type'] = 'application/json'; 则重定向到登录。然后,我去了视图 (resources/view/vendor/l5-swagger/index.blade.php) 并设置了正确的 headers:

    requestInterceptor: function() {
      this.headers['Authorization'] = 'Bearer '+token; 
      this.headers['Content-Type'] = 'application/json';
      this.headers['Accept'] = 'application/json';
      return this;
    }

问题是,当我 "retry it out" 结果是重定向到再次登录。然后我在 curl 上传递 headers:

 curl -X GET "http://127.0.0.1:8000/api/mediador/xxx" -H "accept: */*" -H "Authorization: Bearer tokenGenerated" -H "Content-Type: application/json" -H "Accept: application/json"

这里有一个例子,说明我如何在评论中记录我的电话:

/**
 * Insertamos un nuevo mediador
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse Respuesta formateada para la api del create
 *
 * @OA\Post(
 *     path="/api/mediador/",
 *     tags={"Mediador"},
 *     description="Insert data from mediador profile",
 *     @OA\RequestBody(
 *       required=true,
 *       @OA\MediaType(
 *           mediaType="application/json",
 *           @OA\Schema(
 *               type="object",
 *               required={"id_user_crm","email","nombre_usuario"},
 *               @OA\Property(
 *                   property="id_user_crm",
 *                   description="ID from sugar ",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="email",
 *                   description="Email from profile",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="nombre_usuario",
 *                   description="Name of user",
 *                   type="string"
 *               )
 *           )
 *       )
 *   ),
 *   @OA\Response(response="200", description="This is ok"),
 *   @OA\Response(response="401", description="Not logged with OAUTH token"),
 *   @OA\Response(response="403", description="Wrong data or duplicated fields in DataBase"),
 * )

为什么重定向到登录呢?我如何设置数据

我从我的问题中找到了解决方案:

Swagger 尝试读作 html 如果你没有给 Response 正确的 "mediaType"。这里放一个完整的评论来使用 Swagger ui:

 /** 
 * @OA\Post(
 *     path="/api/mediador/",
 *     tags={"Mediador"},
 *     description="Insert data from mediador profile",
 *     @OA\RequestBody(
 *       required=true,
 *       @OA\MediaType(
 *           mediaType="application/json",
 *           @OA\Schema(
 *               type="object",
 *               required={"id_user_crm","email","nombre_usuario"},
 *               @OA\Property(
 *                   property="id_user_crm",
 *                   description="ID outside of this platform",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="email",
 *                   description="Email from profile",
 *                   type="string"
 *               ),
 *               @OA\Property(
 *                   property="nombre_usuario",
 *                   description="Name of user",
 *                   type="string"
 *               )
 *           )
 *       )
 *   ),
 *   @OA\Response(
 *     response="200", description="Inserted data from mediador profiles",
 *     content={
 *          @OA\MediaType(
 *              mediaType="application/json",
 *              @OA\Schema(
 *                  @OA\Property(
 *                           property="id",
 *                           type="integer",
 *                           description="The id generated"
 *                   ),
 *                  @OA\Property(
 *                         property="id_user_crm",
 *                         type="integer",
 *                         description="The reference id with the other platform"
 *                   )
 *              )
 *          )
 *     }
 *   ),
 *   @OA\Response(response="401", description="Not logged with OAUTH token"),
 *   @OA\Response(response="403", description="Wrong data or duplicated fields in DataBase"),
 * )
 */

在函数前添加此注释,阅读 @OA\Response 并搜索 @OA\MediaType 以了解 returns API REST 的响应类型。