Skip to content

Commit b10be61

Browse files
committed
Setting Content-Type: text/plain for non-Django DELETE responses
1 parent 116da9f commit b10be61

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

Diff for: restless/fl.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flask import make_response
22
from flask import request
33

4+
from .constants import OK, NO_CONTENT
45
from .resources import Resource
56

67

@@ -44,9 +45,14 @@ def is_debug(self):
4445
from flask import current_app
4546
return current_app.debug
4647

47-
def build_response(self, data, status=200):
48+
def build_response(self, data, status=OK):
49+
if status == NO_CONTENT:
50+
# Avoid crashing the client when it tries to parse nonexisting JSON.
51+
content_type = 'text/plain'
52+
else:
53+
content_type = 'application/json'
4854
return make_response(data, status, {
49-
'Content-Type': 'application/json'
55+
'Content-Type': content_type,
5056
})
5157

5258
@classmethod

Diff for: restless/it.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22

33
import itty
4+
5+
from restless.constants import OK, NO_CONTENT
46
from restless.resources import Resource
57

68

@@ -16,8 +18,13 @@ class IttyResource(Resource):
1618
def is_debug(self):
1719
return self.debug
1820

19-
def build_response(self, data, status=200):
20-
return itty.Response(data, status=status, content_type='application/json')
21+
def build_response(self, data, status=OK):
22+
if status == NO_CONTENT:
23+
# Avoid crashing the client when it tries to parse nonexisting JSON.
24+
content_type = 'text/plain'
25+
else:
26+
content_type = 'application/json'
27+
return itty.Response(data, status=status, content_type=content_type)
2128

2229
@classmethod
2330
def setup_urls(cls, rule_prefix):

Diff for: restless/pyr.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from pyramid.response import Response
22

3+
from .constants import OK, NO_CONTENT
34
from .resources import Resource
45

6+
57
class PyramidResource(Resource):
68
"""
79
A Pyramid-specific ``Resource`` subclass.
@@ -26,8 +28,13 @@ def _wrapper(request):
2628

2729
return _wrapper
2830

29-
def build_response(self, data, status=200):
30-
resp = Response(data, status_code=status, content_type="application/json")
31+
def build_response(self, data, status=OK):
32+
if status == NO_CONTENT:
33+
# Avoid crashing the client when it tries to parse nonexisting JSON.
34+
content_type = 'text/plain'
35+
else:
36+
content_type = 'application/json'
37+
resp = Response(data, status_code=status, content_type=content_type)
3138
return resp
3239

3340
@classmethod

Diff for: restless/tnd.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from tornado import web, gen
2-
from .constants import OK
2+
from .constants import OK, NO_CONTENT
33
from .resources import Resource
44
from .exceptions import MethodNotImplemented, Unauthorized
55

@@ -128,8 +128,14 @@ def request_method(self):
128128
def request_body(self):
129129
return self.request.body
130130

131-
def build_response(self, data, status=200):
132-
self.ref_rh.set_header("Content-Type", "application/json; charset=UTF-8")
131+
def build_response(self, data, status=OK):
132+
if status == NO_CONTENT:
133+
# Avoid crashing the client when it tries to parse nonexisting JSON.
134+
content_type = 'text/plain'
135+
else:
136+
content_type = 'application/json'
137+
self.ref_rh.set_header("Content-Type", "{}; charset=UTF-8"
138+
.format(content_type))
133139

134140
self.ref_rh.set_status(status)
135141
self.ref_rh.finish(data)

0 commit comments

Comments
 (0)