如何从具有不同参数的单个端点获得多个响应?

Men*_*hak 22 apiary.io apiblueprint

我们正在考虑使用API​​蓝图.在某些情况下,我们希望一个请求返回正确的响应,另一个请求返回"错误"响应,例如,400 bad request以便其他开发人员可以在apiary.io上使用两种类型的响应来处理模拟API并处理它他们的应用.

我在下面创建了一个完全随意的例子,

## Thing [/thing/{id}]
Gets a thing but the thing id must be a prime number!


+ Parameters
    + id (string) ... ID of the thing, a prime number!

+ Model (application/json)

    The thing itself.

    + Body

            {
                "description": "It is green"
            }

### Retrieve a Single Gist [GET]
+ Response 200

    [Gist][]
Run Code Online (Sandbox Code Playgroud)

现在我想以某种方式添加回复 /thing/40

+ Response 400
    {  "error" : "Invalid request" }
Run Code Online (Sandbox Code Playgroud)

但我不确定如何使用API​​ Blueprint执行此操作.这可以在apiary.io的"旧"风格下实现,但我们想继续使用新的语法

Zde*_*nek 12

要记录多个响应,只需将其添加到Response 200类似之后即可:

## Thing [/thing/{id}]
Gets a thing but the thing id must be a prime number!

+ Parameters
    + id (string) ... ID of the thing, a prime number!

+ Model (application/json)

    The thing itself.

    + Body

            {
                "description": "It is green"
            }

### Retrieve a Single Gist [GET]
+ Response 200

    [Thing][]

+ Response 400 (application/json)

        {  "error" : "Invalid request" }
Run Code Online (Sandbox Code Playgroud)

注意,目前没有专门的语法来讨论条件(返回此响应时).你可以讨论它,无论如何你喜欢它,例如:

+ Response 400 (application/json)

    This response is returned when no `Thing` for given `id` exists.

    + Body
Run Code Online (Sandbox Code Playgroud)

如果您正在使用Apiary mock,请记住默认情况下会返回列出的第一个响应,除非您另外使用prefer HTTP标头.

  • 计划是在API Blueprint的未来版本中添加对它的支持,主要是为了提高蓝图的可测试性 - https://github.com/apiaryio/api-blueprint/issues/21,https://gist.github.com/ zdne/01e287fe18d232672d43 #new-payload-structure-in-1b您可以在https://github.com/apiaryio/api-blueprint/issues/milestones上查看并参与API蓝图路线图. (3认同)
  • 多个请求/响应对现在可用作["多个事务"](https://github.com/apiaryio/api-blueprint/blob/master/API%20Blueprint%20Specification.md#example-multiple-transaction-examples)在API蓝图规范中. (3认同)