top | item 45262553

(no title)

RUnconcerned | 5 months ago

There's nothing stopping an HTTP API from returning both HTML and JSON from the same endpoint. Just have the client send "text/html" or "application/json" in the Accept header, depending on what it needs.

discuss

order

zffr|5 months ago

One challenge is that JSON is for data and HTML is for UI. When a client gets JSON it can transform the data to make it ready for the UI. With HTML, the client is locked into the UI chosen by the server.

What if the client wants to render the output of an API in different ways depending on what screen is visible? If the server API outputs JSON, this is trivial. If it outputs HTML the client is stuck into rendering what the server gives it.

Izkata|5 months ago

That's why GP mentioned the Accept header, the client can choose at runtime which one it wants.

lucyjojo|5 months ago

i don't do frontend so apologies if it's a dumb question.

if html is for ui, what is css for?

i always thought html was for documents/data (like xml light)

cenamus|5 months ago

However it would make sense to have a separate json api for other applications. That way the html endpoints can change without without api versioning, perfectly matching whatever the gui needs.

jonkoops|5 months ago

There absolutely is, this is just extra cruft you need to maintain, and who says that the HTML is universal enough to be used everywhere? This is exactly where a front-end or a backend-for-frontend (BFF) makes sense.