Writes the given chunk to the output buffer. To write the output to the network, use the `flush()` method below. If the given chunk is a dictionary, we write it as JSON and set the Content-Type of the response to be ``application/json``. (if you want to send JSON as
(self, chunk: Union[str, bytes, dict])
| 807 | self.finish() |
| 808 | |
| 809 | def write(self, chunk: Union[str, bytes, dict]) -> None: |
| 810 | """Writes the given chunk to the output buffer. |
| 811 | |
| 812 | To write the output to the network, use the `flush()` method below. |
| 813 | |
| 814 | If the given chunk is a dictionary, we write it as JSON and set |
| 815 | the Content-Type of the response to be ``application/json``. |
| 816 | (if you want to send JSON as a different ``Content-Type``, call |
| 817 | ``set_header`` *after* calling ``write()``). |
| 818 | |
| 819 | Note that lists are not converted to JSON because of a potential |
| 820 | cross-site security vulnerability. All JSON output should be |
| 821 | wrapped in a dictionary. More details at |
| 822 | http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ and |
| 823 | https://github.com/facebook/tornado/issues/1009 |
| 824 | """ |
| 825 | if self._finished: |
| 826 | raise RuntimeError("Cannot write() after finish()") |
| 827 | if not isinstance(chunk, (bytes, unicode_type, dict)): |
| 828 | message = "write() only accepts bytes, unicode, and dict objects" |
| 829 | if isinstance(chunk, list): |
| 830 | message += ( |
| 831 | ". Lists not accepted for security reasons; see " |
| 832 | + "http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.write" # noqa: E501 |
| 833 | ) |
| 834 | raise TypeError(message) |
| 835 | if isinstance(chunk, dict): |
| 836 | chunk = escape.json_encode(chunk) |
| 837 | self.set_header("Content-Type", "application/json; charset=UTF-8") |
| 838 | chunk = utf8(chunk) |
| 839 | self._write_buffer.append(chunk) |
| 840 | |
| 841 | def render(self, template_name: str, **kwargs: Any) -> "Future[None]": |
| 842 | """Renders the template with the given arguments as the response. |
no test coverage detected