Apiary.io - multiple responses (200) with different parameters
Asked Answered
E

2

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

Erdrich answered 18/6, 2015 at 9:37 Comment(0)
T
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.

Teenyweeny answered 18/6, 2015 at 11:54 Comment(5)
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.Erdrich
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?Erdrich
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).Teenyweeny
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.Teenyweeny
Thank you. I really believe this url should be more clear: support.apiary.io/knowledgebase/articles/…Erdrich
E
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"
        } 
Erdrich answered 19/6, 2015 at 10:35 Comment(1)
Yes this would work to if you do not mind having multiple alike resource in the documentationTeenyweeny

© 2022 - 2024 — McMap. All rights reserved.