2016-02-15 14 views
14

Tôi muốn Đăng một cơ thể json với Swagger, như thế này:Đăng một cơ thể json với vênh vang

curl -H "Content-Type: application/json" -X POST -d {"username":"foobar","password":"xxxxxxxxxxxxxxxxx", "email": "[email protected]"}' http://localhost/user/register 

Hiện nay, tôi có định nghĩa này:

"/auth/register": { 
     "post": { 
      "tags": [ 
       "auth" 
      ], 
      "summary": "Create a new user account", 
      "parameters": [ 
       { 
        "name": "username", 
        "in": "query", 
        "description": "The username of the user", 
        "required": true, 
        "type": "string" 
       }, 
       { 
        "name": "password", 
        "in": "query", 
        "description": "The password of the user", 
        "required": true, 
        "type": "string", 
        "format": "password" 
       }, 
       { 
        "name": "email", 
        "in": "query", 
        "description": "The email of the user", 
        "required": true, 
        "type": "string", 
        "format": "email" 
       } 
      ], 
      "responses": { 
       "201": { 
        "description": "The user account has been created", 
        "schema": { 
         "$ref": "#/definitions/User" 
        } 
       }, 
       "default": { 
        "description": "Unexpected error", 
        "schema": { 
         "$ref": "#/definitions/Errors" 
        } 
       } 
      } 
     } 
    } 

Nhưng các dữ liệu được gửi đi trong URL. Ở đây, tạo curl cung cấp bởi Swagger:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://localhost/user/register?username=foobar&password=password&email=foo%40bar.com' 

Tôi hiểu rằng query keywork là không tốt, nhưng tôi không tìm thấy cách để Đăng một cơ thể JSON. Tôi đã thử formData nhưng nó không hoạt động.

Trả lời

29

Bạn cần phải sử dụng body tham số:

"parameters": [ 
     { 
     "in": "body", 
     "name": "body", 
     "description": "Pet object that needs to be added to the store", 
     "required": false, 
     "schema": { 
      "$ref": "#/definitions/Pet" 
     } 
     } 
    ], 

#/definitions/Pet được định nghĩa là một mô hình:

"Pet": { 
    "required": [ 
    "name", 
    "photoUrls" 
    ], 
    "properties": { 
    "id": { 
     "type": "integer", 
     "format": "int64" 
    }, 
    "category": { 
     "$ref": "#/definitions/Category" 
    }, 
    "name": { 
     "type": "string", 
     "example": "doggie" 
    }, 
    "photoUrls": { 
     "type": "array", 
     "xml": { 
     "name": "photoUrl", 
     "wrapped": true 
     }, 
     "items": { 
     "type": "string" 
     } 
    }, 
    "tags": { 
     "type": "array", 
     "xml": { 
     "name": "tag", 
     "wrapped": true 
     }, 
     "items": { 
     "$ref": "#/definitions/Tag" 
     } 
    }, 
    "status": { 
     "type": "string", 
     "description": "pet status in the store", 
     "enum": [ 
     "available", 
     "pending", 
     "sold" 
     ] 
    } 
    }, 
    "xml": { 
    "name": "Pet" 
    } 
}, 

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.json#L81

OpenAPI/Swagger spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object

+0

Nó hoàn hảo, cảm ơn! – ncrocfer

+0

Làm thế nào để tạo mô hình trong laravel-5.5? –

Các vấn đề liên quan