简体   繁体   中英

swagger php pass parameters as json

So i have comment like below in my php code, so basically i am trying to generate below kind of request which my api is accepting.

I have comments like below:

/**
* 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.",
*),    
* )
*/

Expected Output

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"}'

Actual Output

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\"}"

In Swagger-ui when i am trying to execute it it gives me the error:

TypeError: Failed to fetch

What i have tried

  1. Change consumes to "multipart/form-data" (Same error)
  2. If i change in="body" to in="query" then its working, but i can't use that. As its required to pass params in POST method.
  3. Might not be related, but in console i am getting below error:

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)

So what should i do to fix this issue?

Change in="body", to in="formData", . Voila.

Remove the default attribute from your Swagger parameter and rather set them in your swagger schema.

@SWG\Schema(
    @SWG\Property(property="username", type="string", example="abc@abc.com"),
    @SWG\Property(property="password", type="string", example="12345678")
)

Else, if you wish to use swagger definition, set swagger property as above in your definition.

It is resolved by combining the answers provided by @Pusparaj and @delboy1978uk

Basically i have to change the in="body" , to in="formData"

After that i have to change my comment like below:

Earlier

*@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")
*),

Updated

@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")
    )
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM