What would be correct/preferred REST api design to fetch data that depends on timezone?
I was thinking about simply adding timezone as an URL param, so:
$ curl www.api.example.com/data?timezone="Europe/Amsterdam"
But someone suggested to use custom header for that in similar manner like github does:
$ curl -H "X-Time-Zone: Europe/Amsterdam" www.api.example.com/data
https://developer.github.com/v3/#using-the-time-zone-header
I wonder what are benefits of using header here instead of param? Is there any design pattern behind such decision?
The REST headers and parameters contain a wealth of information that can help you track down issues when you encounter them. HTTP Headers are an important part of the API request and response as they represent the meta-data associated with the API request and response.
The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes.
The user time zone is a client-specific time zone that can be defined for the user time and user date of each individual user in the user master record. It is contained in the system field sy-zonlo.
It's entirely up to you, whether you wish to accept an input parameter in the path, querystring, header, or content body.
However, one could argue that a header makes sense when it is not the primary concern of the api you're calling. For example, if your api was specifically to retrieve the properties of a time zone, then I would recommend passing it on the url, either on the querystring as you suggested in your question, or on the path (www.api.example.com/timezone/europe/amsterdam).
Also note that the example you gave that uses the parameter in a header is a POST call. Though it's not prohibited, it usually doesn't make a lot of sense to pass querystring parameters in a POST. By passing the time zone as a header, the body of the message can focus on what is being posted.
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