File tree 4 files changed +35
-9
lines changed
4 files changed +35
-9
lines changed Original file line number Diff line number Diff line change 1
1
from flask import make_response
2
2
from flask import request
3
3
4
+ from .constants import OK , NO_CONTENT
4
5
from .resources import Resource
5
6
6
7
@@ -44,9 +45,14 @@ def is_debug(self):
44
45
from flask import current_app
45
46
return current_app .debug
46
47
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'
48
54
return make_response (data , status , {
49
- 'Content-Type' : 'application/json'
55
+ 'Content-Type' : content_type ,
50
56
})
51
57
52
58
@classmethod
Original file line number Diff line number Diff line change 1
1
import re
2
2
3
3
import itty
4
+
5
+ from restless .constants import OK , NO_CONTENT
4
6
from restless .resources import Resource
5
7
6
8
@@ -16,8 +18,13 @@ class IttyResource(Resource):
16
18
def is_debug (self ):
17
19
return self .debug
18
20
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 )
21
28
22
29
@classmethod
23
30
def setup_urls (cls , rule_prefix ):
Original file line number Diff line number Diff line change 1
1
from pyramid .response import Response
2
2
3
+ from .constants import OK , NO_CONTENT
3
4
from .resources import Resource
4
5
6
+
5
7
class PyramidResource (Resource ):
6
8
"""
7
9
A Pyramid-specific ``Resource`` subclass.
@@ -26,8 +28,13 @@ def _wrapper(request):
26
28
27
29
return _wrapper
28
30
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 )
31
38
return resp
32
39
33
40
@classmethod
Original file line number Diff line number Diff line change 1
1
from tornado import web , gen
2
- from .constants import OK
2
+ from .constants import OK , NO_CONTENT
3
3
from .resources import Resource
4
4
from .exceptions import MethodNotImplemented , Unauthorized
5
5
@@ -128,8 +128,14 @@ def request_method(self):
128
128
def request_body (self ):
129
129
return self .request .body
130
130
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 ))
133
139
134
140
self .ref_rh .set_status (status )
135
141
self .ref_rh .finish (data )
You can’t perform that action at this time.
0 commit comments