@@ -674,9 +674,14 @@ def has_header(self, key: str) -> bool:
674
674
return key .lower () in (k .lower () for k in self .headers .keys ())
675
675
676
676
def text (
677
- self , content : JSONType , status_code : int = 200 , headers : Dict [str , Any ] = {}
677
+ self ,
678
+ content : JSONType ,
679
+ status_code : Optional [int ] = None ,
680
+ headers : Dict [str , Any ] = {},
678
681
):
679
682
"""Send plain text or HTML content."""
683
+ if status_code is None :
684
+ status_code = self ._status_code
680
685
new_response = PlainTextResponse (
681
686
body = content , status_code = status_code , headers = headers
682
687
)
@@ -686,12 +691,14 @@ def text(
686
691
def json (
687
692
self ,
688
693
data : Union [str , List [Any ], Dict [str , Any ]],
689
- status_code : int = 200 ,
694
+ status_code : Optional [ int ] = None ,
690
695
headers : Dict [str , Any ] = {},
691
696
indent : Optional [int ] = None ,
692
697
ensure_ascii : bool = True ,
693
698
):
694
699
"""Send JSON response."""
700
+ if status_code is None :
701
+ status_code = self ._status_code
695
702
new_response = JSONResponse (
696
703
content = data ,
697
704
status_code = status_code ,
@@ -714,14 +721,23 @@ def set_permanent_cookie(
714
721
self .set_cookie (key , value , expires = expires , ** kwargs ) # type:ignore
715
722
return self
716
723
717
- def empty (self , status_code : int = 200 , headers : Dict [str , Any ] = {}):
724
+ def empty (self , status_code : Optional [ int ] = None , headers : Dict [str , Any ] = {}):
718
725
"""Send an empty response."""
726
+ if status_code is None :
727
+ status_code = self ._status_code
719
728
new_response = BaseResponse (status_code = status_code , headers = headers )
720
729
self ._response = self ._preserve_headers_and_cookies (new_response )
721
730
return self
722
731
723
- def html (self , content : str , status_code : int = 200 , headers : Dict [str , Any ] = {}):
732
+ def html (
733
+ self ,
734
+ content : str ,
735
+ status_code : Optional [int ] = None ,
736
+ headers : Dict [str , Any ] = {},
737
+ ):
724
738
"""Send HTML response."""
739
+ if status_code is None :
740
+ status_code = self ._status_code
725
741
new_response = HTMLResponse (
726
742
content = content , status_code = status_code , headers = headers
727
743
)
@@ -743,6 +759,7 @@ def file(
743
759
content_disposition_type = content_disposition_type ,
744
760
)
745
761
self ._response = self ._preserve_headers_and_cookies (new_response )
762
+ self ._response .status_code = self ._status_code
746
763
return self
747
764
748
765
def stream (
@@ -752,6 +769,8 @@ def stream(
752
769
status_code : Optional [int ] = None ,
753
770
):
754
771
"""Send streaming response."""
772
+ if status_code is None :
773
+ status_code = self ._status_code
755
774
new_response = StreamingResponse (
756
775
content = iterator , # type: ignore
757
776
status_code = status_code or self ._status_code ,
@@ -772,11 +791,11 @@ def redirect(self, url: str, status_code: int = 302):
772
791
def status (self , status_code : int ):
773
792
"""Set response status code."""
774
793
self ._response .status_code = status_code
794
+ self ._status_code = status_code
775
795
return self
776
796
777
797
def set_header (self , key : str , value : str , overide : bool = False ):
778
798
"""Set a response header."""
779
-
780
799
self ._response .set_header (key , value , overide = overide )
781
800
return self
782
801
0 commit comments