Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a RESTful architecture, how should clients request the format in which a resource is to be consumed?

Tags:

rest

Let's say I have a website with listings of people. When a "person" resource is accessed via a browser, the server returns the data for that resource in HTML format. However if I also want to make that resource consumable via a computer, what is the preferred method of allowing a client to request the data in JSON format as opposed to HTML?

There are three ways that I can think of right now that this can be implemented.

1.) I've seen many websites open up their APIs via a sub-path (eg, /api/people/john-doe), but that seems to be anti-restful as it creates multiple endpoints for the same resource.

2.) Another way would be to pass in the query-string arguments (eg, /people/john-doe?format=json), but is that the right place for it?

3.) A custom HTTP header could be used in the request, eg: X-Response-Format: json. This seems like the cleanest way to me, but again, not sure if it's the right place for it.

like image 270
Ben Davis Avatar asked Jan 21 '26 04:01

Ben Davis


1 Answers

I quickly found out after a little more Google-ing that the correct method is the Accept header.

like image 174
Ben Davis Avatar answered Jan 23 '26 20:01

Ben Davis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!