繁体   English   中英

招摇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:无法获取

我尝试过的

  1. 更改消耗为“ multipart / form-data”(相同错误)
  2. 如果我将in =“ body”更改为in =“ query”,则它可以工作,但我不能使用它。 由于需要在POST方法中传递参数。
  3. 可能不相关,但是在控制台中我遇到以下错误:

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)处

那我该怎么做才能解决这个问题呢?

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

暂无
暂无

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

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