@@ -404,9 +404,10 @@ class HttpBody:
404404 __type_binary = 2
405405 __type_multipart = 3
406406
407- def __init__ (self , type : int = 0 , payload = None ):
407+ def __init__ (self , type : int = 0 , payload = None , charset = None ):
408408 self ._type = type
409409 self ._payload = payload
410+ self ._charset = charset
410411
411412 @classmethod
412413 def of (cls , data = None ):
@@ -424,7 +425,7 @@ def of(cls, data = None):
424425 else :
425426 type = cls .__type_none
426427 payload = None
427- return cls (type , payload )
428+ return cls (type , payload , 'utf-8' )
428429
429430 @classmethod
430431 def parse (cls , dict ):
@@ -433,10 +434,13 @@ def parse(cls, dict):
433434 type = dict ['type' ]
434435 if type == cls .__type_none :
435436 payload = None
437+ charset = None
436438 elif type == cls .__type_text :
437- payload = dict ['payload' ]
439+ payload = dict ['payload' ]['text' ]
440+ charset = dict ['payload' ]['charset' ]
438441 elif type == cls .__type_binary :
439442 payload = dict ['payload' ]
443+ charset = None
440444 if isinstance (payload , str ):
441445 with open (payload , mode = 'rb' ) as file :
442446 payload = file .read ()
@@ -446,9 +450,10 @@ def parse(cls, dict):
446450 payload = bytes ()
447451 elif type == cls .__type_multipart :
448452 payload = []
453+ charset = None
449454 for multipart in dict ['payload' ]:
450455 payload .append (HttpMultipartBody (multipart ))
451- return cls (type , payload )
456+ return cls (type , payload , charset )
452457
453458 def __repr__ (self ):
454459 if self .isMultipart :
@@ -611,9 +616,15 @@ def serialize(self):
611616 payload = None
612617 type = HttpBody .__type_none
613618 else :
614- payload = self ._payload
619+ payload = {
620+ 'text' : self ._payload ,
621+ 'charset' : self ._charset
622+ }
615623 else :
616- payload = json .dumps (self ._payload )
624+ payload = {
625+ 'text' : json .dumps (self ._payload ),
626+ 'charset' : self ._charset
627+ }
617628 elif self .isBinary :
618629 if len (self ._payload ) == 0 :
619630 type = HttpBody .__type_none
0 commit comments