Skip to content

Commit

Permalink
Setting Content-Type: text/plain for non-Django DELETE responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSaico committed Jun 28, 2016
1 parent 116da9f commit b10be61
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
10 changes: 8 additions & 2 deletions restless/fl.py
@@ -1,6 +1,7 @@
from flask import make_response
from flask import request

from .constants import OK, NO_CONTENT
from .resources import Resource


Expand Down Expand Up @@ -44,9 +45,14 @@ def is_debug(self):
from flask import current_app
return current_app.debug

def build_response(self, data, status=200):
def build_response(self, data, status=OK):
if status == NO_CONTENT:
# Avoid crashing the client when it tries to parse nonexisting JSON.
content_type = 'text/plain'
else:
content_type = 'application/json'
return make_response(data, status, {
'Content-Type': 'application/json'
'Content-Type': content_type,
})

@classmethod
Expand Down
11 changes: 9 additions & 2 deletions restless/it.py
@@ -1,6 +1,8 @@
import re

import itty

from restless.constants import OK, NO_CONTENT
from restless.resources import Resource


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

def build_response(self, data, status=200):
return itty.Response(data, status=status, content_type='application/json')
def build_response(self, data, status=OK):
if status == NO_CONTENT:
# Avoid crashing the client when it tries to parse nonexisting JSON.
content_type = 'text/plain'
else:
content_type = 'application/json'
return itty.Response(data, status=status, content_type=content_type)

@classmethod
def setup_urls(cls, rule_prefix):
Expand Down
11 changes: 9 additions & 2 deletions restless/pyr.py
@@ -1,7 +1,9 @@
from pyramid.response import Response

from .constants import OK, NO_CONTENT
from .resources import Resource


class PyramidResource(Resource):
"""
A Pyramid-specific ``Resource`` subclass.
Expand All @@ -26,8 +28,13 @@ def _wrapper(request):

return _wrapper

def build_response(self, data, status=200):
resp = Response(data, status_code=status, content_type="application/json")
def build_response(self, data, status=OK):
if status == NO_CONTENT:
# Avoid crashing the client when it tries to parse nonexisting JSON.
content_type = 'text/plain'
else:
content_type = 'application/json'
resp = Response(data, status_code=status, content_type=content_type)
return resp

@classmethod
Expand Down
12 changes: 9 additions & 3 deletions restless/tnd.py
@@ -1,5 +1,5 @@
from tornado import web, gen
from .constants import OK
from .constants import OK, NO_CONTENT
from .resources import Resource
from .exceptions import MethodNotImplemented, Unauthorized

Expand Down Expand Up @@ -128,8 +128,14 @@ def request_method(self):
def request_body(self):
return self.request.body

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

self.ref_rh.set_status(status)
self.ref_rh.finish(data)
Expand Down

0 comments on commit b10be61

Please sign in to comment.