php - SwaggerUI 传递参数
问题描述
我有一个像这样定义的 swagger.json:
"/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 采用如下参数:
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: ic
并
Notice: Undefined index: id
返回错误。
我已经为这两个参数尝试了所有这三个:
"in": "path",
"in": "query",
"in": "body",
这是在我的服务中调用 api 的方式:
$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
可以是值数组:
"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
}
],
但仍然有同样的错误......:-(
解决方案
将参数更改为此使其工作。我正在使用 OAS 2.0
"parameters": [
{
"name": "id",
"in": "formData",
"description": "ID to retrieve Tiered inventory for",
"required": true,
"type": "string"
},
{
"name": "ic",
"in": "formData",
"description": "IC to retrieve Tiered inventory for",
"required": true,
"type": "string"
}
],
推荐阅读
- c# - 在 ASP.Net Core 中获取基本 URL
- java - 来自java的API调用,奇怪的结果?
- android - 为Android Button添加箭头的最简单方法是什么
- python - 通过 Python (Spyder IDE) 连接到 Oracle
- python - 如何使用python从csv文件中读取数组列表
- javascript - Angular 6,Materialize:初始化javascript
- sql - 单个空格的正则表达式,然后是数字
- matplotlib - ModuleNotFoundError: No module named 'mpl_toolkits.basemap' 如何安装底图
- mapkit - 苹果地图不显示地图覆盖
- c++ - 在基类中存储指向派生类函数的指针