API Responses – Why Return XML/JSON for API Requests Instead of Directly Processing and Returning Modified Web Page?

apijsonxml

Let's say that a user requests for some piece of data. Usually the data is processed via a server and then returned in XML/JSON format. Im guessing another piece of processing happens to convert it into HTML/CSS so that it could be displayed on a browser.

Why do we bother returning XML/JSON for API requests? Why can't we just directly process whatever the user wants and just return the modified web page (HTML/CSS/JS)?

Best Answer

Because you can do many more things with real data besides display it in a web page.

HTML is just a type of output that web browsers recognize, and it's not really useable as data in a meaningful way, because its metadata is about structure and form, not about the data itself. It also contains all sorts of things that have nothing to do with data output, such as the <input> tag.

XML and JSON, on the other hand, can be used to create dynamic HTML web pages by simply binding to it. In addition, you can create reports with it, you can analyze it with programs and algorithms, you can meaningfully put it in a database and process it further.

None of these things are really practical with HTML, since HTML is a markup language, not a data transport mechanism.