SwaggerUI passing parameters not working

39 views
Skip to first unread message

Eddie Shipman

unread,
Jun 5, 2018, 11:35:22 AM6/5/18
to Swagger
I have a swagger.json defined like this:

    "/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": "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
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation"
                    },
                    "500": {
                        "description": "Internal error"
                    }
                }
            }
        }
    },

Now, my API takes the parameters like this:

    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 $this->toJSON($rows);
    }

It doesn't matter what the in value is, I still get these errors returned:
Notice: Undefined index: ic
Notice: Undefined index: id

I have tried all three of these for both parameters:

    "in": "path"
    "in": "query"
    "in": "body"

This is how the api function is called from my service:

    $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);

In reviewing the Request headers of the test, I see that the querystring only contains the rquest variable which determines which function is being called. 

The request method is POST and the Content-Type is set to application/x-www-form-urlencoded. However, if I set the in value to body, I still get the error. 
I can't have it as path because I may have an array of ic values.

Can anyone help me get this thing working correctly?


MrBaseball34

unread,
Jul 2, 2018, 11:31:55 AM7/2/18
to Swagger
Solved by changing "body" in below to "formData"
Reply all
Reply to author
Forward
0 new messages