首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >SwaggerUI传递参数

SwaggerUI传递参数
EN

Stack Overflow用户
提问于 2018-06-04 21:34:00
回答 1查看 2.9K关注 0票数 8

我有一个定义如下的swagger.json:

代码语言:javascript
复制
"/API/GetTieredInventory": {
        "post": {
            "summary": "Get Tiered inventory from ID",
            "description": "Gets Tiered inventory for passed ID/IC combination",
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "description": "ID to retrieve Tiered inventory for",
                    "required": true,
                    "type": "string"
                },
                {
                    "name": "ic",
                    "in": "path",
                    "description": "IC to retrieve Tiered inventory for",
                    "required": true,
                    "type": "string"
                }
            ],
            "responses": {
                "200": {
                    "description": "successful operation"
                },
                "500": {
                    "description": "Internal error"
                }
            }
        }
    }
},

现在,我的API接受如下参数:

代码语言:javascript
复制
private function GetTieredInventory() {
    if($this->get_request_method() != "POST"){
        $this->response('Error code 405, Method not allowed.',405);
    }
    if(is_array($this->_request['ic'])) {
        $v = array();       
        foreach($this->_request['ic'] as $i) {
            $v[] = "'".$i."'";
        }
        $ic=html_entity_decode(implode(',', $v ));
    } else {
        $ic = "'".$this->_request['ic']."'";
    }
    $id=pg_escape_string($this->_request['id']);

    <SNIP DB CODE>

    try 
    {       
        $results = pg_query($sql);
        if(pg_num_rows($results) == 0) {
            $rows = [];
        }
        else
        {
            $data = pg_fetch_all($results);
            foreach($data as $item)
            {                    
                $rows[$item["ic"]][] = $item;
            }
        }
        pg_free_result($results);
    }
    catch (Exception $e) 
    {
        $err = array("message"=>$e->getMessage(), "code"=> $e->getCode(), "error"=>$e->__toString().",\n".print_r($_REQUEST, true));
        $this->response($this->toJSON($err),500);
    }
    echo json_encode($rows);
}

不管in的值是什么,我仍然会得到一个

返回Notice: Undefined index: icNotice: Undefined index: id错误。

我已经为这两个参数尝试了所有这三个参数:

代码语言:javascript
复制
"in": "path",
"in": "query",
"in": "body",

下面是api在我的服务中的调用方式:

代码语言:javascript
复制
$fields = array(
    'id' => $this->compId,
    'ic'    => $ic
); 
$fields_string = http_build_query($fields);

$url = "/API/GetTieredInventory";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

更改了这样的参数定义,因为 ic 可以是值的数组:

代码语言:javascript
复制
"parameters": [
    {
        "name": "id",
        "in": "body",
        "description": "ID to retrieve Tiered inventory for",
        "required": true,
        "type": "string"
    },
    {
        "name": "ic",
        "in": "body",
        "schema": {
            "type": "array",
            "items": {
                "type": "string",
                "style": "form"
            }
        },
        "description": "Interchange to retrieve Tiered inventory for",
        "required": true
    }
],

但是仍然有相同的错误...:-(

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50681892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档