简体   繁体   English

招摇PHP传递参数为JSON

[英]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. 所以我在我的php代码中有如下注释,所以基本上我正在尝试生成以下我的api接受的请求。

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: 在Swagger-ui中,当我尝试执行它时,出现了以下错误:

TypeError: Failed to fetch TypeError:无法获取

What i have tried 我尝试过的

  1. Change consumes to "multipart/form-data" (Same error) 更改消耗为“ multipart / form-data”(相同错误)
  2. If i change in="body" to in="query" then its working, but i can't use that. 如果我将in =“ body”更改为in =“ query”,则它可以工作,但我不能使用它。 As its required to pass params in POST method. 由于需要在POST方法中传递参数。
  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) root-injects.js:95 TypeError:e.get(...)。entrySeq不是t.default(curl.jsx:17)的t.default(curlify.js:23)的函数(t.render( u._renderValidatedComponentWithoutOwnerOrContext(ReactCompositeComponent.js:796)处的root-injects.js:93)u._performComponentUpdate(ReactCompositeComponent.ReactCompositeComponent.ReactCompositeComponent.ReactCompositeComponent.ReactCompositeComponent。 721)在Object.receiveComponent(ReactReconciler.js:122)的u.receiveComponent(ReactCompositeComponent.js:544)的updateComponent(ReactCompositeComponent.js:642)处

So what should i do to fix this issue? 那我该怎么做才能解决这个问题呢?

Change in="body", to in="formData", . in="body",更改为in="formData", Voila. 瞧。

Remove the default attribute from your Swagger parameter and rather set them in your swagger schema. 从Swagger参数中删除默认属性,而是在swagger模式中进行设置。

@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. 否则,如果您希望使用swagger定义,请在定义中如上所述设置swagger属性。

It is resolved by combining the answers provided by @Pusparaj and @delboy1978uk 通过组合@Pusparaj和@ delboy1978uk提供的答案来解决

Basically i have to change the in="body" , to in="formData" 基本上我必须将in="body"更改为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")
    )
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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