swagger php 将参数作为 json 传递
swagger php pass parameters as json
所以我在我的 php 代码中有如下评论,所以基本上我试图生成以下我的 api 正在接受的请求。
我有如下评论:
/**
* Login API
*
*
*@SWG\Definition(
* definition="login",
* description="Enter your username",
*)
*@SWG\Definition(
* definition="password",
* type="string",
* description="Enter your Password",
*)
* @SWG\Post(
* path="/user/login",
* description="Login API.",
* operationId="Login",
* produces={"application/json"},
* tags={"login"},
* consumes={"application/json"},
*@SWG\Parameter(
* name="params",
* in="body",
* type="string",
* default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
* description="Login Detail",
* required=true,
* @SWG\Schema(ref="#/definitions/login")
*),
*@SWG\Response(
* response=200,
* description="Token overview."
*),
*@SWG\Response(
* response=401,
* description="Unauthorized action.",
*),
* )
*/
预期输出
curl -X POST \
http://localhost/project/api/web/user/login \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 78219bf9-85a4-420f-7637-55e2f0e51b11' \
-F 'params={"username":"abc@abc.com","password":"12345678"}'
实际输出
curl -X POST "http://localhost/project/api/web/user/login" -H "accept: application/json" -H "Content-Type: application/json" -d "params={\"username\":\"abc@abc.com\",\"password\":\"12345678\"}"
在 Swagger-ui 中,当我尝试执行它时,出现错误:
TypeError: Failed to fetch
我试过的
- 将消耗更改为 "multipart/form-data"(同样的错误)
- 如果我将 in="body" 更改为 in="query" 那么它可以工作,但我不能使用它。作为其要求 uired 在 POST 方法中传递参数。
- 可能不相关,但在控制台中出现以下错误:
root-injects.js:95 TypeError: e.get(...).entrySeq is not a function
at t.default (curlify.js:23)
at t.value (curl.jsx:17)
at t.render (root-injects.js:93)
at u._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:796)
at u._renderValidatedComponent (ReactCompositeComponent.js:819)
at u._updateRenderedComponent (ReactCompositeComponent.js:743)
at u._performComponentUpdate (ReactCompositeComponent.js:721)
at updateComponent (ReactCompositeComponent.js:642)
at u.receiveComponent (ReactCompositeComponent.js:544)
at Object.receiveComponent (ReactReconciler.js:122)
那么我应该怎么做才能解决这个问题?
海牙 in="body",
in="formData",
。子弹.
从您的 Swagger 参数中删除默认属性,而是在您的 swagger 模式中设置它们。
@SWG\Schema(
@SWG\Property(property="username", type="string", example="abc@abc.com"),
@SWG\Property(property="password", type="string", example="12345678")
)
否则,如果您想使用 swagger 定义,请在您的定义中像上面那样设置 swagger 属性。
结合@Pusparaj 和@delboy1978uk 提供的答案解决
基本上我必须将 in="body"
更改为 in="formData"
之后我必须像下面这样更改我的评论:
早些时候
*@SWG\Parameter(
* name="params",
* in="body",
* type="string",
* default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
* description="Login Detail",
* required=true,
* @SWG\Schema(ref="#/definitions/login")
*),
已更新
@SWG\Parameter(
name="params",
in="formData",
type="string",
default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
description="Login Detail",
required=true,
@SWG\Schema(
@SWG\Property(property="username", type="string"),
@SWG\Property(property="password", type="string")
)
),
所以我在我的 php 代码中有如下评论,所以基本上我试图生成以下我的 api 正在接受的请求。
我有如下评论:
/**
* Login API
*
*
*@SWG\Definition(
* definition="login",
* description="Enter your username",
*)
*@SWG\Definition(
* definition="password",
* type="string",
* description="Enter your Password",
*)
* @SWG\Post(
* path="/user/login",
* description="Login API.",
* operationId="Login",
* produces={"application/json"},
* tags={"login"},
* consumes={"application/json"},
*@SWG\Parameter(
* name="params",
* in="body",
* type="string",
* default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
* description="Login Detail",
* required=true,
* @SWG\Schema(ref="#/definitions/login")
*),
*@SWG\Response(
* response=200,
* description="Token overview."
*),
*@SWG\Response(
* response=401,
* description="Unauthorized action.",
*),
* )
*/
预期输出
curl -X POST \
http://localhost/project/api/web/user/login \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 78219bf9-85a4-420f-7637-55e2f0e51b11' \
-F 'params={"username":"abc@abc.com","password":"12345678"}'
实际输出
curl -X POST "http://localhost/project/api/web/user/login" -H "accept: application/json" -H "Content-Type: application/json" -d "params={\"username\":\"abc@abc.com\",\"password\":\"12345678\"}"
在 Swagger-ui 中,当我尝试执行它时,出现错误:
TypeError: Failed to fetch
我试过的
- 将消耗更改为 "multipart/form-data"(同样的错误)
- 如果我将 in="body" 更改为 in="query" 那么它可以工作,但我不能使用它。作为其要求 uired 在 POST 方法中传递参数。
- 可能不相关,但在控制台中出现以下错误:
root-injects.js:95 TypeError: e.get(...).entrySeq is not a function at t.default (curlify.js:23) at t.value (curl.jsx:17) at t.render (root-injects.js:93) at u._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:796) at u._renderValidatedComponent (ReactCompositeComponent.js:819) at u._updateRenderedComponent (ReactCompositeComponent.js:743) at u._performComponentUpdate (ReactCompositeComponent.js:721) at updateComponent (ReactCompositeComponent.js:642) at u.receiveComponent (ReactCompositeComponent.js:544) at Object.receiveComponent (ReactReconciler.js:122)
那么我应该怎么做才能解决这个问题?
海牙 in="body",
in="formData",
。子弹.
从您的 Swagger 参数中删除默认属性,而是在您的 swagger 模式中设置它们。
@SWG\Schema(
@SWG\Property(property="username", type="string", example="abc@abc.com"),
@SWG\Property(property="password", type="string", example="12345678")
)
否则,如果您想使用 swagger 定义,请在您的定义中像上面那样设置 swagger 属性。
结合@Pusparaj 和@delboy1978uk 提供的答案解决
基本上我必须将 in="body"
更改为 in="formData"
之后我必须像下面这样更改我的评论:
早些时候
*@SWG\Parameter(
* name="params",
* in="body",
* type="string",
* default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
* description="Login Detail",
* required=true,
* @SWG\Schema(ref="#/definitions/login")
*),
已更新
@SWG\Parameter(
name="params",
in="formData",
type="string",
default="params={""username"":""abc@abc.com"",""password"":""12345678""}",
description="Login Detail",
required=true,
@SWG\Schema(
@SWG\Property(property="username", type="string"),
@SWG\Property(property="password", type="string")
)
),