Best practice for returning partial responses?

Options

Hi there everyone!

First time posting here and I'm still in the early stages of my learning journey with everything backend…

I really like human friendly RESTful API design and want to figure out how best to create an endpoint whose response changes depending on a querystring / or param value?

I've got a URI named /courses. I'd like the default response to return just a minimal list of course info e.g. ID and Name. Fine on it's own, but I'm struggling to figure out if an optional parameter was suppled e.g. /courses?expanded=true then I'd like to return a list of courses with all the course fields: ID, Name, Description, Created_at etc.

Alternatively, an approach similar to what Google offer works well: REST calls for partial responses take this format: e.g. GET https://library.googleapis.com/v1/shelves?$fields=shelves.name

which returns only a list of shelves.name values.

What are my options?

Looking forward to your responses and congrats on such a great platform!

Antony

Comments

  • Lachlan
    Lachlan Administrator

    ADMIN

    Options

    Hi there hopefully this helps you with your query :)

    https://www.loom.com/share/992b9551d7904df8b50a7fbddd54afbc

  • ribotmaximus
    ribotmaximus Member
    Options

    Amazing, thank you for the Loom recording @Lachlan. Really appreciate you showing a few options. What I think I'm going to do now is the following:

    1. Use an add-on to expand the default response to cover a number of useful data points.
    2. Use an optional input parameter to provide a comma delimited list of fields to show
    3. Loop through the records and extract the relevant fields into an array

    Many thanks for your help! I'll get back with any additional questions or learnings.

    Antony