Skip to content

Commit 579345d

Browse files
authored
Make sure 204 status responses have no body in accordance with HTTP spec (geopython#2094)
* Fix provider no data error * fix flake8 * address feedback * fix gzip issue which adds a header and deviates from the spec
1 parent d633b3b commit 579345d

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

pygeoapi/api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ def get_exception(self, status, headers, format_, code,
604604
else:
605605
content = to_json(exception, self.pretty_print)
606606

607+
if status == HTTPStatus.NO_CONTENT:
608+
LOGGER.error('HTTP 204 detected, suppressing content')
609+
content = ''
610+
607611
return headers, status, content
608612

609613
def get_format_exception(self, request) -> Tuple[dict, int, str]:

pygeoapi/starlette_app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
""" Starlette module providing the route paths to the api"""
3434

3535
import asyncio
36+
from http import HTTPStatus
3637
import os
3738
from typing import Callable, Union
3839
from pathlib import Path
@@ -133,9 +134,10 @@ async def execute_from_starlette(api_function, request: Request, *args,
133134
headers, status, content = await loop.run_in_executor(
134135
None, call_api_threadsafe, loop, api_function,
135136
actual_api, api_request, *args)
136-
# NOTE: that gzip currently doesn't work in starlette
137-
# https://github.com/geopython/pygeoapi/issues/1591
138-
content = apply_gzip(headers, content)
137+
# 204 responses must have an empty body, but gzip
138+
# encoding would add gzip metadata, thus we skip
139+
if status != HTTPStatus.NO_CONTENT:
140+
content = apply_gzip(headers, content)
139141

140142
response = _to_response(headers, status, content)
141143

tests/api/test_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,10 @@ def test_get_exception(config, api_):
847847
assert content['code'] == 'NoApplicableCode'
848848
assert content['description'] == 'oops'
849849

850-
d = api_.get_exception(500, {}, 'html', 'NoApplicableCode', 'oops')
850+
d = api_.get_exception(204, {}, 'html', 'NoApplicableCode', 'oops')
851+
assert len(d[2]) == 0
852+
d = api_.get_exception(204, {}, 'json', 'NoApplicableCode', 'oops')
853+
assert len(d[2]) == 0
851854

852855

853856
def test_evaluate_limit():

0 commit comments

Comments
 (0)