Search code examples
javascriptswaggerswagger-uiopenapi

how to change name of a header in runtime in Openapi 3


I have an api in which a key:value is sent as a header to the server. I wonder whether i can create a dynamic header that its name is filled during run time by the client? I run the JSON file by swagger-ui application.

My API looks like this:

curl -x GET $theUrl -h "customKey:customValue"

I want the client write the key (which is name of the header in the JSON file) same as value in the run time. Even JS solutions are welcome.

This is the OpenAPI 3 Specification in JSON:

"post": {
            "tags": [
                "Object APIs"
            ],
            "summary": "Create or update object metadata",
            "operationId": "CrUp_Object_Metadata",
            "parameters": [
                {
                    "$ref": "#/components/parameters/X-AUTH-Token"
                },
                {
                    "$ref": "#/components/parameters/Account"
                },
                {
                    "$ref": "#/components/parameters/Container"
                },
                {
                    "$ref": "#/components/parameters/Object"
                },
                {
                    "name": "X-Object-Meta-Book",
                    "in": "header",
                    "required": true,
                    "style": "simple",
                    "schema": {
                        "type": "string",
                        "example": "goodbye"
                    }
                }
            ],
            "responses": {
                "202": {
                    "description": "Successful",
                    "headers": {}
                }
            }
        }

As you can see, I have a "Post" request. I want to change name of header "X-Object-Meta-Book" to a custom name. by that i can post name:value to the server.

here is the picture: https://i.sstatic.net/VJWML.jpg


Solution

  • There is no solution to this problem within OpenAPI 3 itself; headers are maps with the names declared as plain strings (see headers: https://spec.openapis.org/oas/v3.1.0#headerObject).

    This is because the whole point of OpenAPI is to outline the server's well-defined capabilities to the clients.

    To express how your server accepts and handles client-defined headers, you can utilize openapi-extensions or provide a textual description (you can use markdown and emoji therein).

    On a sidenote, HTTP response code 202 does not mean "Successful", you should fix that.