2017-02-14 18 views
0

У меня возникли проблемы с отправкой параметров тела через Python-2.7, связь & Pycharm.Параметры тела Swagger Returns 400 «Дополнительные параметры параметра formaata (не) в спецификации«

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() s возвращение к

'Product Added', 200 

еще возвращает 400, так что проблема это, очевидно, на уровне связи.

Оценка:

ответ

0

Решено.

Я забыл включить словарь Python в строку (для тела), так

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

возвращает 200

Python словарей используются для отправки данных формы; данные тела необходимо отправить в виде строки JSON.