Skip to content

Script Development / Response Data DFF.RESP

You can directly return plain JSON or text responses. When you need to control the HTTP status code, headers, download file name, or content type, use DFF.RESP(...).

For disk files, use DFF.RESP_FILE(...); for MB-level data, use DFF.RESP_LARGE_DATA(...).

Parameters

Parameter Type Required / Default Description
data Any JSON-serializable value Required Response body
status_code int 200 HTTP status code
content_type str None Response body type, e.g., json, text, html
headers dict None Additional response headers; no need to include Content-Type
allow_304 bool False Whether to allow the browser to use 304 caching
download bool / str False True generates a default file name; a string specifies the file name; once enabled, content_type no longer takes effect
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def html_response():
    return DFF.RESP('<h1>Hello</h1>', content_type='html')

def json_response():
    return DFF.RESP('{"hello": "world"}', content_type='json')

def download_response():
    return DFF.RESP('Some text', download='article.txt')

def header_response():
    return DFF.RESP('ok', headers={'X-Author': 'Tom'})

def not_found_response():
    return DFF.RESP({'error': 'not found'}, status_code=404)

When constructing a response, whether data can be serialized is verified. If serialization fails, or if download is used together with data=None, the Func response will fail.

You must directly return the response object created by DFF.RESP(...); embedding it in other dictionaries or lists will not apply the status code, headers, or content type.

Warning

allow_304=True may let clients obtain cached content, so it should not be enabled when data changes frequently. Custom response headers must not contain secrets or other sensitive information.