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

时间:2014-01-22 09:39:59

标签: 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][]

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

的回复
+ Response 400
    {  "error" : "Invalid request" }

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

2 个答案:

答案 0 :(得分: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" }

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

+ Response 400 (application/json)

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

    + Body

如果您使用Apiary模拟,请记住默认情况下会返回列出的第一个响应,除非您另有说明使用prefer HTTP header

答案 1 :(得分:1)

您可以使用模板并在资源的一般响应后指定特定用例,请参阅示例:

预订[/ reservation / {reservation_key}]

Reservation对象具有以下属性:

  • room_number
  • reserved_at - 问题发布时的ISO8601日期。
  • booker_details - 一个布克对象。

  • 参数

    • reservation_key(required,text,1)... Reservation Key ash

查看预订详情[GET]

  • 回复200(application / json)

    {
        "room_number": 55,
        "reserved_at": "2014-11-11T08:40:51.620Z",
        "booker_details": 
            {
                "name": "Jon",
                "last_name": "Doe",
            }
    }
    

预订[/ reservation / notarealreservation123]

使用不存在的预订(请使用假的notarealreservation123)返回未找到

相关问题