RAML中的POST参数支持

时间:2016-02-16 16:55:37

标签: api rest raml

我想询问RAML中是否支持POST参数。如果有 - 什么是语法。我已经粗略地浏览了规范0.8和规范1.0(实际上我已经0.8绑定了,因为很多工具还不支持1.0 )。我没有找到POST参数支持,但也许我只是错过了一些东西。

那么POST参数是什么意思?这些可以是两者之一(对不起,我不知道他们的正式名称,如果有的话):

  • HTTP普通参数key=value,一行中的每个参数,例如

    name=John Doe amount=5 这不是很方便(例如没有嵌套)

  • 参数作为JSON对象,只是一个允许其所有语法的JSON(服务器端需要解析这个json);如:

    {"name":"John Doe","amount":"5"}

不同的服务器端API实现使用第一个或第二个。无论如何, RAML如何支持这些?

3 个答案:

答案 0 :(得分:7)

正如本参考文献https://github.com/raml-org/raml-spec/wiki/Breaking-Changes所示:

对于raml 0.8:

body:
  application/x-www-form-urlencoded:
    formParameters:
      name:
        description: name on account
        type: string
        example: Naruto Uzumaki
      gender:
        enum: ["male", "female"]

raml 1.0中的等效项是否为:

body:
  application/x-www-form-urlencoded:
    properties:
      name:
        description: name on account
        type: string
        example: Naruto Uzumaki
      gender:
        enum: ["male", "female"]

所以它改变的是一个属性的formParameters属性。

答案 1 :(得分:6)

  

@Pedro已经涵盖了选项2,所以这里是选项1.根据评论中的讨论,似乎使用的编码是application/x-www-form-urlencoded

您需要使用formParameters

示例:

  post:
    description: The POST operation adds an object to a specified bucket using HTML forms.
    body:
      application/x-www-form-urlencoded:
        formParameters:
          AWSAccessKeyId:
            description: The AWS Access Key ID of the owner of the bucket who grants an Anonymous user access for a request that satisfies the set of constraints in the Policy.
            type: string
          acl:
            description: Specifies an Amazon S3 access control list. If an invalid access control list is specified, an error is generated.
            type: string

参考:https://github.com/raml-org/raml-spec/blob/master/raml-0.8.md#web-forms

答案 2 :(得分:4)

可以使用JSON Schema

表示发布参数

简单的RAML 0.8示例:

#%RAML 0.8
title: Api
baseUri: /
schemas:
  - Invoice: |
    { 
      "$schema": "http://json-schema.org/draft-03/schema",
      "type": "object",
      "properties": {
        "Id": { "type": "integer"},
        "Name": { "type": "string"},
        "Total": { "type": "number"}
      }
    }
/invoices:
  post:
    body:
      application/json:
        schema: Invoice
相关问题