Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing configuration settings with a RESTful API?

Tags:

rest

I'm designing a front-end to modify the settings of the web application I'm developing. The settings are stored like so:

{
    "site_title": "My Web Site",
    "site_description_long": "Welcome to My Web Site! Lots of welcome text here!",
    "site_description_short": "It's my site"
}

I'm very new to REST APIs so I'm not sure how to proceed. It's not like every other resource like Users and Posts where there are many objects. There is only one Settings object with unlimited numbers of keys and values. Is it bad practice for /settings to return a single object? Is it bad practice to access it by name and just return a string like /settings/site_title? Would that even be necessary when the client can just cache the entire settings object client-side?

like image 553
Brian Ortiz Avatar asked Sep 25 '13 04:09

Brian Ortiz


People also ask

What is API configuration setting?

The configuration API provides a central place for modules to store configuration data. This data can be simple configuration like your site name, or more complex information managed with configuration entities, such as views and content types.

How do you interact with RESTful APIs?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.

What can you do with RESTful API?

A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.


1 Answers

One option would be to treat each setting like its own resource.

GET /settings/
{
    settings: [
        { "id":"site_title" , "value":"My Web Site" },
        { "id":"site_description_short" , "value":"It's my site" },
    ]
}

GET /settings/site_title
{
    "id":"site_title", 
     "value":"My Web Site"
}

This approach is RESTful, but will lead to a lot of server hits. I think using a single object as you describe is not unreasonable, although if you have 500 settings it can become painful to deal with.

like image 155
Eric Stein Avatar answered Oct 03 '22 12:10

Eric Stein