REST是仅创建资源表示还是创建并传输?浏览器是否渲染该表示?
Hey there! These are great questions—REST's focus on "representations" can feel fuzzy at first, so let's unpack them clearly.
1. Does REST only create a resource's Representation, or does it handle both creation and transmission of that Representation?
Here's the deal: REST is an architectural style, not a single tool or protocol, but in standard RESTful interactions (like over HTTP), both creating the representation and transmitting it are core parts of the process.
When a client sends a request (say, a GET for a blog post), the server first does the work to generate the resource's representation: it might fetch data from a database, format it into a standard structure like JSON, XML, or HTML, and make sure it adheres to any agreed-upon media types (like application/json).
Once that representation exists, the server then transmits it back to the client via the HTTP response (including headers that tell the client what kind of representation it's getting). You can't have one without the other—creating the representation is useless if it never reaches the client, and transmission can't happen unless the server first builds that structured version of the resource.
2. Is the Representation only created, or is it created, transmitted, and then rendered by the browser?
This depends entirely on what type of representation the server sends:
- If the representation is HTML: The server creates the HTML (maybe using a template engine to inject dynamic data into an HTML template), transmits it to the browser, and then the browser takes over to parse and render that HTML into the visual webpage you see.
- If the representation is data (like JSON/XML): The server creates and transmits the data, but the browser won't automatically render it into a webpage. Instead, your frontend code (like JavaScript with React, Vue, or vanilla JS) will receive that data and handle rendering—either by building DOM elements directly or using a framework to generate the UI from the data.
Crucially, REST doesn't dictate anything about rendering—that's the client's responsibility. The server's job is just to provide a valid, consistent representation of the resource; what the client (browser) does with it after receiving it is up to the client's implementation.
内容的提问来源于stack exchange,提问作者jstacx12

