Question

I'm developing an API for returning either a single product or a collection of products, so will have endpoints like http://api.company.com/products/ and http://api.company.com/products/1/ that return data in the following structures:

// http://api.company.com/products/1/
{
    Id: 1
    Name: "Product 1"
}

// http://api.company.com/products/
[
    {
        Id: 1,
        Name: "Product 1"
    },
    {
        Id: 2,
        Name: "Product 2"
    }
]

I'm currently using the following media types in the Accept header, respectively:

application/vnd.company.product-v1.0+json // single product
application/vnd.company.products-v1.0+json // collection of products

Now it doesn't seem ideal to have two media types with "product" and "products" as I can see that leading to some confusion, but they do return different structures of data. So I'm wondering if there's any kind of API standard here that dictates whether you should only use one media type for both or not?

Était-ce utile?

La solution

I do not have a link to a "REST API Standard" that dictates whether what you propose is acceptable or not. However, of the many REST APIs I've had to deal with, I have not seen one that used the accept header like you are proposing.

What happens if you later allow flags on GET /products to include different levels of details about the products? You would need different Accept header values (example application/vnd.company.products-details-v1.0+json vs application/vnd.company.products-min-v1.0+json)

I believe you are better off with just one Accept header value.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top