11

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

MeV
  • 3,337
  • 8
  • 33
  • 74

2 Answers2

3

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.

Zdenek
  • 3,523
  • 25
  • 34
  • Thank you Zdenek for your reply. I am still not able to understand how to implement my API, even reading the "Invoking non-default-responses". Would you be able to provide an example please? I believe there is quite a few people looking for this example. – MeV Jun 18 '15 at 12:10
  • The only way to invoke a non-default response is using a different code and type of response, therefore I assume is not possible to return two responses with code 200 and type application/json, is that right? – MeV Jun 18 '15 at 12:37
  • Important thing is to understand Apiary does not provide implementation of an API. It gives you merely a mock server for prototyping purposes. The implementation is really up to you (albeit you can and should test it with tools like Dred). – Zdenek Jun 18 '15 at 14:05
  • In API Blueprint – description of your API – you can have as many responses as you want including the same status code ones. For the same statues the rule is the request have to differ. But again Apiary mock will return you only the first one if not asked explicitly using the preferred HTTP header. – Zdenek Jun 18 '15 at 14:06
  • Thank you. I really believe this url should be more clear: http://support.apiary.io/knowledgebase/articles/117119-handling-multiple-actions-on-a-single-resource – MeV Jun 19 '15 at 09:31
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"
        } 
MeV
  • 3,337
  • 8
  • 33
  • 74
  • 3
    Yes this would work to if you do not mind having multiple alike resource in the documentation – Zdenek Jun 19 '15 at 11:36