Skip to content

Script Development / Response Data DFF.RESP

The return value of a function, in addition to directly returning strings or JSON as before, can be controlled in detail using DFF.RESP(...).

Parameter Type Required / Default Description
data str/dict/list Required Specifies the data to be returned
status_code int 200 Specifies the response status code
content_type str None Specifies the response body type, such as json, text, html, etc.
headers dict None Specifies the HTTP response headers (no need to repeat Content-Type here)
allow_304 bool False When set to True, allows browser 304 caching
download str False Specifies the download filename and downloads the data as a file
When this parameter is specified, the content_type parameter is ignored

If allow_304 is enabled, allowing browser 304 caching can improve interface performance. However, it may also cause the client to fail to get the latest content from the interface in time due to caching.

When the download parameter is specified, the system will automatically fill in the Content-Type based on the file extension, and the content_type parameter will be ignored.

Common use cases are as follows:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@DFF.API('Use Case 1')
def case_1():
    '''
    Returns an HTML page generated within the function
    '''
    data = '''<h1>Hello, World!</h1>'''
    return DFF.RESP(data, content_type='html')

@DFF.API('Use Case 2')
def case_2():
    '''
    Returns JSON data generated by the function
    Equivalent to return {"hello": "world"}
    '''
    data = '''{"hello": "world"}'''
    return DFF.RESP(data, content_type='json')

@DFF.API('Use Case 3')
def case_3():
    '''
    Downloads a file generated by the function and names it `article.txt`
    '''
    data = '''Some text'''
    return DFF.RESP(data, download='article.txt')

@DFF.API('Use Case 4')
def case_4():
    '''
    Specifies additional response headers
    '''
    data = '''<h1>Hello, World!</h1>'''
    headers = {
        'X-Author': 'Tom',
    }
    return DFF.RESP(data, content_type='html', headers=headers)

@DFF.API('Use Case 5')
def case_5():
    '''
    Specifies the response code
    '''
    data = '''<h1>No such data</h1>'''
    return DFF.RESP(data, content_type='html', status_code=404)