Python 大摇大摆身体参数返回400“;额外formData参数名称不在规范中;

Python 大摇大摆身体参数返回400“;额外formData参数名称不在规范中;,python,pycharm,yaml,swagger,Python,Pycharm,Yaml,Swagger,我无法通过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

我无法通过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"
}
将addProduct()的返回更改为

'Product Added', 200  
仍然返回400,因此问题显然在连接级别

非常感谢

解决了

我忘了将Python字典转换为字符串(用于正文),所以

返回200

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

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