Skip to content
Myr

Merge pull request #938 from ahlen/response-with-headers-without-status

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Enable tuple responses with headers without status

authored by Armin Ronacher · committed by Armin Ronacher · parents 4d9916e, dbc4096 · Browse files · 3 files+47 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+47 added−12 removed

  • other:+47 added−12 removed

3 files+47 added−12 removed

modified:docs/quickstart.rst+3 added-3 removed
674 unmodified lines
6752. If it's a string, a response object is created with that data and the676 default parameters.6773. If a tuple is returned the items in the tuple can provide extra678 information. Such tuples have to be in the form ``(response, status,679 headers)`` where at least one item has to be in the tuple. The680 `status` value will override the status code and `headers` can be a681 list or dictionary of additional header values.679 headers)`` or ``(response, headers)`` where at least one item has680 to be in the tuple. The `status` value will override the status code681 and `headers` can be a list or dictionary of additional header values.6824. If none of that works, Flask will assume the return value is a683 valid WSGI application and convert that into a response object.684685If you want to get hold of the resulting response object inside the view
167 unmodified lines
modified:flask/testsuite/basic.py+30 added
733 unmodified lines
734 def from_tuple():735 return 'Meh', 400, {736 'X-Foo': 'Testing',737 'Content-Type': 'text/plain; charset=utf-8'738 } 739 @app.route('/two_args')740 def from_two_args_tuple():741 return 'Hello', {742 'X-Foo': 'Test',743 'Content-Type': 'text/plain; charset=utf-8'744 }745 @app.route('/args_status')746 def from_status_tuple():747 return 'Hi, status!', 400748 @app.route('/args_header')749 def from_response_instance_status_tuple():750 return flask.Response('Hello world', 404), {751 "X-Foo": "Bar",752 "X-Bar": "Foo"753 }754755 c = app.test_client()756 self.assert_equal(c.get('/unicode').data, u'Hällo Wörld'.encode('utf-8'))757 self.assert_equal(c.get('/string').data, u'Hällo Wörld'.encode('utf-8'))758 rv = c.get('/args')759 self.assert_equal(rv.data, b'Meh')760 self.assert_equal(rv.headers['X-Foo'], 'Testing')761 self.assert_equal(rv.status_code, 400)762 self.assert_equal(rv.mimetype, 'text/plain')763 rv2 = c.get('/two_args')764 self.assert_equal(rv2.data, b'Hello')765 self.assert_equal(rv2.headers['X-Foo'], 'Test')766 self.assert_equal(rv2.status_code, 200)767 self.assert_equal(rv2.mimetype, 'text/plain')768 rv3 = c.get('/args_status')769 self.assert_equal(rv3.data, b'Hi, status!')770 self.assert_equal(rv3.status_code, 400)771 self.assert_equal(rv3.mimetype, 'text/html')772 rv4 = c.get('/args_header')773 self.assert_equal(rv4.data, b'Hello world')774 self.assert_equal(rv4.headers['X-Foo'], 'Bar')775 self.assert_equal(rv4.headers['X-Bar'], 'Foo')776 self.assert_equal(rv4.status_code, 404)777778 def test_make_response(self):779 app = flask.Flask(__name__)780 with app.test_request_context():
554 unmodified lines
modified:flask/app.py+14 added-9 removed
1542 unmodified lines
1543 string encoded to utf-8 as body1544 a WSGI function the function is called as WSGI application1545 and buffered as response object1546 :class:`tuple` A tuple in the form ``(response, status,1547 headers)`` where `response` is any of the1547 headers)`` or ``(response, headers)``1548 where `response` is any of the1549 types defined here, `status` is a string1550 or an integer and `headers` is a list or1551 a dictionary with header values.1552 ======================= ===========================================
3 unmodified lines
1556 .. versionchanged:: 0.91557 Previously a tuple was interpreted as the arguments for the1558 response object.1559 """1559 status = headers = None1560 status_or_headers = headers = None1561 if isinstance(rv, tuple):1561 rv, status, headers = rv + (None,) * (3 - len(rv))1562 rv, status_or_headers, headers = rv + (None,) * (3 - len(rv))15631564 if rv is None:1565 raise ValueError('View function did not return a response')15661567 if isinstance(status_or_headers, (dict, list)):1568 headers, status_or_headers = status_or_headers, None15691570 if not isinstance(rv, self.response_class):1571 # When we create a response object directly, we let the constructor1572 # set the headers and status. We do this because there can be1573 # some extra logic involved when creating these objects with1574 # specific values (like default content type selection).1575 if isinstance(rv, (text_type, bytes, bytearray)):1572 rv = self.response_class(rv, headers=headers, status=status)1573 headers = status = None1576 rv = self.response_class(rv, headers=headers, status=status_or_headers)1577 headers = status_or_headers = None1578 else:1579 rv = self.response_class.force_type(rv, request.environ)15801577 if status is not None:1578 if isinstance(status, string_types):1579 rv.status = status1581 if status_or_headers is not None:1582 if isinstance(status_or_headers, string_types):1583 rv.status = status_or_headers1584 else:1581 rv.status_code = status1585 rv.status_code = status_or_headers1586 if headers:1587 rv.headers.extend(headers)15881589 return rv159015911592 def create_url_adapter(self, request):1593 """Creates a URL adapter for the given request. The URL adapter1594 is created at a point where the request context is not yet set up1595 so the request is passed explicitly.
250 unmodified lines