How is the output format encoded in a RESTful URL?

Suppose I have a RESTful web service that contains information about an object that can be accessed at a URL, such as http://example.com/myobject . I would like to receive this information in two formats: firstly, simple data in XML format, and secondly, as a full HTML page, which can also include a javascript interface for modifying an object and PUT it back with AJAX.

What is the canonical way to achieve this? Do I have to publish my object in two different URLs, for example http://example.com/myobject?format=xml and ... format = html? (Are there any more efficient ways than using the query string to distinguish URLs here?) Or is it wise to send something like multi-page MIME data and I can rely on the ability of the browser to extract part of the HTML? Or is there some kind of HTTP header field in the request that I could use?

(With PUT or POST requests sent in different formats, this is much simpler, since the server can check the format and parse it accordingly.)

+3
source share
3 answers

. Accept header (, "application/xml, text/html; q = 0,9" ), ( )

Vary, ,

, Accept.

+4

, , , :

http://example.com/myobject.html
http://example.com/myobject.xml
http://example.com/myobject.json
etc.

, , , . text/html HTML, text/xml XML, text/json JSON .. :

- , HTTP, (, , ) URI, .

URL- , HTML-, ( ).

+2

You will find that you want one HTML page to manage several or more XML resources, not just one. So stick with your HTML form at example.com/stuff and your XML resources at example.com/api/thing, an object, etc. Alternatively, if you want XML to be the main navigation, stick with this example.com/things, etc and your HTML at example.com/things/manager.html or something similar. It is now even easier that Firefox and Chrome have plugins that allow user users to click on hyperlinks in non-HTML types.

0
source

All Articles