Skip to content

Script Development / Response Data DFF.RESP

The return value of the function, in addition to directly returning strings or JSON as before, can use DFF.RESP(...) for detailed control.

Parameter Type Required / Default Value 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 type of response body, 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 file name and sends the data as a file download
After specifying this parameter, the content_type parameter will no longer take effect

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 due to caching.

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

Common usage examples 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('Example 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('Example 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('Example 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('Example 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('Example 5')
def case_5():
    '''
    Specifies the status code
    '''
    data = '''<h1>No such data</h1>'''
    return DFF.RESP(data, content_type='html', status_code=404)