Apiary.io - multiple responses (200) with differen

2020-03-01 16:10发布

问题:

I am trying to get different responses passing different parameters but something is not working.

This is my API:

## Question [/questions/{question_id}]

A Question object has the following attributes:

+ Parameters
    + question_id: `1` (number, required) - ID of the Question in form of an integer

### View a Questions Detail [GET]

+ Request

+ Header

    X-Custom-Header : 1

+ Response 200 (application/json)

        {
            "id": "1",
            "name": "Marco"
        }


+ Request

+ Header

    X-Custom-Header : 2

+ Response 200 (application/json)

        {
            "id: "2",
            "name": "Lucas"
        }

But when calling /questions/1 or /questions/2 the response is always the same:

{
    "id": "1",
    "name": "Marco"
}

What is wrong?

Thank you

回答1:

Nothing is wrong with your blueprint. I am afraid the Apiary Mock is rather simplistic and always returns the first response specified (content-negotiation permitting) as default.

See "Invoking non-default responses" at Apiary http://support.apiary.io/knowledgebase/articles/117119-handling-multiple-actions-on-a-single-resource to see how to invoke (on demand) another response.

Also note there is a proposed syntax in API Blueprint to explicitly state what values of parameters are tied to a particular response – https://github.com/apiaryio/api-blueprint/issues/58

However whether will Apiary's mock take benefit of this is unclear at the moment.



回答2:

I believe there is a simple solution to do this without using headers:

Create different resources (one for each record), so each one will generate one URL.

## Note20 [/notes/20]

### Get notes20 [GET]

+ Response 200 (application/json)

        {
            "id" : 20,
            "name" : "note xxxx"
        } 

## Note21 [/notes/21]

### Get notes21 [GET]

+ Response 200 (application/json)

        {
            "id" : 21,
            "name" : "note yyyyy"
        }