描述带有Swagger的API的头参数

描述带有Swagger的API的头参数,swagger,swagger-editor,connexion,Swagger,Swagger Editor,Connexion,我试图使用Swagger为ConnexionAPI(Python+Flask)创建规范。很棒的工具。我知道HTTP请求头**不会作为常规参数传递给处理程序函数,但我需要能够从操作中获取请求头。我读书。我使用了Swagger编辑器来生成一个最小的python服务器(概念验证),但它不能从头开始工作,这可能是需求方面的问题: default requirements.txt不允许我启动服务器,显示以下错误消息: $ python3 -m swagger_server Traceback (most

我试图使用SwaggerConnexionAPI(Python+Flask)创建规范。很棒的工具。我知道HTTP请求头**不会作为常规参数传递给处理程序函数,但我需要能够从操作中获取请求头。我读书。我使用了Swagger编辑器来生成一个最小的python服务器(概念验证),但它不能从头开始工作,这可能是需求方面的问题:

default requirements.txt不允许我启动服务器,显示以下错误消息:

$ python3 -m swagger_server
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/agalindodev/tmp/python-flask-server/swagger_server/__main__.py", line 3, in <module>
    import connexion
  File "/home/agalindodev/tmp/python-flask-server/venv/lib/python3.6/site-packages/connexion/__init__.py", line 3, in <module>
    from .apis import AbstractAPI  # NOQA
  File "/home/agalindodev/tmp/python-flask-server/venv/lib/python3.6/site-packages/connexion/apis/__init__.py", line 1, in <module>
    from .abstract import AbstractAPI  # NOQA
  File "/home/agalindodev/tmp/python-flask-server/venv/lib/python3.6/site-packages/connexion/apis/abstract.py", line 14, in <module>
    from ..operation import Operation
  File "/home/agalindodev/tmp/python-flask-server/venv/lib/python3.6/site-packages/connexion/operation.py", line 7, in <module>
    from .decorators import validation
  File "/home/agalindodev/tmp/python-flask-server/venv/lib/python3.6/site-packages/connexion/decorators/validation.py", line 9, in <module>
    from werkzeug import FileStorage
ImportError: cannot import name 'FileStorage'
这是我的环境:

1。操作系统和运行时:

swagger: "2.0"
basePath: /api
info:
  title: "Just a swagger test API"
  version: "1.0.0"
paths:
  /my_jobs:
    post:
      operationId: my_job.create
      tags:
        - MyJob
      summary: "Create a job"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
        - name: "my_session"
          in: "header"
          description: "Session id that's creating the job"
          required: True
          type: string
      responses:
        "201":
          description: "Successfully created a job"
          schema:
            $ref: "#/definitions/MyJob"
definitions:
  MyJob:
    type: "object"
    properties:
      id:
        type: "string"
Ubuntu 18.04上的python 3.6.9

2。requirements.txt

# connexion == 1.1.15
connexion == 2.4.0
python_dateutil == 2.6.0
setuptools >= 21.0.0
3。完整的招摇过市规范:

swagger: "2.0"
basePath: /api
info:
  title: "Just a swagger test API"
  version: "1.0.0"
paths:
  /my_jobs:
    post:
      operationId: my_job.create
      tags:
        - MyJob
      summary: "Create a job"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
        - name: "my_session"
          in: "header"
          description: "Session id that's creating the job"
          required: True
          type: string
      responses:
        "201":
          description: "Successfully created a job"
          schema:
            $ref: "#/definitions/MyJob"
definitions:
  MyJob:
    type: "object"
    properties:
      id:
        type: "string"
4。错误: 使用修改后的requirements.txt,我只是试图发布一个创建,传递头,但它生成了一个错误:

$ curl -v -X POST --header 'Content-Type: application/json' --header 'Accept: application/problem+json' --header 'my_session: { "id": "xxxxx" }' 'http://0.0.0.0:8080/api/my_jobs'
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8080 (#0)
> POST /api/my_jobs HTTP/1.1
> Host: 0.0.0.0:8080
> User-Agent: curl/7.58.0
> Content-Type: application/json
> Accept: application/problem+json
> my_session: { "id": "xxxxx" }
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 500 INTERNAL SERVER ERROR
< Content-Type: application/problem+json
< Content-Length: 252
< Server: Werkzeug/0.12.2 Python/3.6.9
< Date: Sun, 02 Aug 2020 10:39:58 GMT
< 
{
  "detail": "The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.",
  "status": 500,
  "title": "Internal Server Error",
  "type": "about:blank"
}
* Closing connection 0
我怎样才能让它工作


非常感谢

头参数不会作为参数传递给处理程序函数

你不能用

  parameters:
  - name: "my_session"
    in: "header"
    description: "Session id that's creating the job"
    required: True
    type: string

您必须使用
connexion.request.headers['my_session']

头参数不会作为参数传递给处理程序函数

你不能用

  parameters:
  - name: "my_session"
    in: "header"
    description: "Session id that's creating the job"
    required: True
    type: string

您必须使用
connexion.request.headers['my\u session']

这有帮助吗?还有,海伦。我认为我的问题实际上是一个关于由swagger Editor生成的swagger服务器的新手问题。requirements.txt不适合我。一旦移动到较新版本的Connexion,它就会启动,但随后无法管理头文件。如果你能发布一个workingrequirements.txt,那就太好了。提前谢谢!这有用吗?还有,海伦。我认为我的问题实际上是一个关于由swagger Editor生成的swagger服务器的新手问题。requirements.txt不适合我。一旦移动到较新版本的Connexion,它就会启动,但随后无法管理头文件。如果你能发布一个workingrequirements.txt,那就太好了。提前谢谢!谢谢你的回答,凯文,但这与我在问题中的解释相符,在我的环境中不起作用。我将对这个问题进行编辑,使其更加简洁,给出libs的版本和完整的自负定义。请提供适合您的requirements.txt文件,好吗?由swagger Editor生成的swagger服务器不适用于我,我不得不更改需求,可能这就是问题所在。提前谢谢!谢谢你的回答,凯文,但这与我在问题中的解释相符,在我的环境中不起作用。我将对这个问题进行编辑,使其更加简洁,给出libs的版本和完整的自负定义。请提供适合您的requirements.txt文件,好吗?由swagger Editor生成的swagger服务器不适用于我,我不得不更改需求,可能这就是问题所在。提前谢谢!