【问题标题】:Swagger Body Parameters Returns 400 "Extra formData parameter(s) name not in spec"Swagger Body 参数返回 400“Extra formData parameter(s) name not in spec”
【发布时间】:2017-07-02 10:57:13
【问题描述】:

我无法通过 Python-2.7、connexion 和 Pycharm 发送正文参数。

api.yaml

swagger: '2.0'
info:
  title: Products Api Backend
  version: "1.0.0"
consumes:
  - application/json
produces:
  - application/json
paths:
  /products:
    post:
      operationId: app.addProduct
      parameters:
      - name: body
        description: Product payload to add
        in: body
        required: true
        schema:
          $ref: '#/definitions/ProductParameters'
      responses:
        200:
          description: Data received and added correctly
          schema:
            type: string
definitions:
  ProductParameters:
    description: Needed attributes for each post request
    properties:
      name:
        type: string
        description: Product name

app.py

import connexion

api = connexion.api

def addProduct(name):
   return 'Product Added'   # or 'Product Added', 200

app = connexion.App(__name__)
app.add_api('api.yaml', strict_validation=True)

if __name__ == '__main__':
    app.run(port=8092, debug=True)

跑步

r = requests.post(appUrl, data={'name':'Product title here'})
print r
print r.content

返回

<Response [400]>
{
  "detail": "Extra formData parameter(s) name not in spec", 
  "status": 400, 
  "title": null, 
  "type": "about:blank"
}

YAML 在 Swagger Editor 中验证,但运行发送请求会给出

ERROR Method Not Allowed
Headers
undefined
Body

将 addProduct() 的返回改为

'Product Added', 200  

仍然返回 400,因此问题显然出在连接级别。

非常感谢

【问题讨论】:

    标签: python pycharm yaml swagger connexion


    【解决方案1】:

    解决了。

    我忘了把 Python 字典转成字符串(用于正文),所以

    import json
    dataDict = {'name':'Product title here'}
    dataStr = json.dumps(dataDict)
    r = requests.post(appUrl, data=dataStr)
    

    返回 200

    Python 字典用于发送表单数据;正文数据需要作为 JSON 字符串发送。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2020-02-08
      • 2018-10-14
      • 2013-03-19
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多