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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With