Skip to content

RESPONSE_NULL_HANDLING ​

Also known as

response_null (with or without @ prefix)

Control how NULL results are returned in plain text responses when the execution returns NULL from the database.

Syntax ​

code
@response_null <mode>
@response_null_handling <mode>

Values ​

ValueAliasDescription
empty_stringemptyReturns an empty response body with status code 200 OK (default)
null_literalnullReturns the string literal null with status code 200 OK
no_content204, 204_no_contentReturns status code 204 NO CONTENT

Examples ​

Return Empty String for NULL ​

sql
sql
comment on function get_value(_id int) is
'HTTP GET
@response_null empty_string';

If result is NULL → empty response body (200 OK)

Return 204 for NULL ​

sql
sql
comment on function find_record(_id int) is
'HTTP GET
@response_null 204_no_content';

If result is NULL → HTTP 204 with no body

Return JSON null ​

sql
sql
comment on function get_optional(_key text) is
'HTTP GET
@response_null null_literal';

If result is NULL → Response body: null

Configuration Default ​

You can set the default behavior for all endpoints in appsettings.json:

json
json
{
  "NpgsqlRest": {
    "TextResponseNullHandling": "NoContent"
  }
}

Available values: EmptyString (default), NullLiteral, NoContent.

This sets the default for all endpoints, which can then be overridden per-endpoint using comment annotations.

Comments