@@ -404,9 +404,10 @@ class HttpBody:
404
404
__type_binary = 2
405
405
__type_multipart = 3
406
406
407
- def __init__ (self , type : int = 0 , payload = None ):
407
+ def __init__ (self , type : int = 0 , payload = None , charset = None ):
408
408
self ._type = type
409
409
self ._payload = payload
410
+ self ._charset = charset
410
411
411
412
@classmethod
412
413
def of (cls , data = None ):
@@ -424,7 +425,7 @@ def of(cls, data = None):
424
425
else :
425
426
type = cls .__type_none
426
427
payload = None
427
- return cls (type , payload )
428
+ return cls (type , payload , 'utf-8' )
428
429
429
430
@classmethod
430
431
def parse (cls , dict ):
@@ -433,10 +434,13 @@ def parse(cls, dict):
433
434
type = dict ['type' ]
434
435
if type == cls .__type_none :
435
436
payload = None
437
+ charset = None
436
438
elif type == cls .__type_text :
437
- payload = dict ['payload' ]
439
+ payload = dict ['payload' ]['text' ]
440
+ charset = dict ['payload' ]['charset' ]
438
441
elif type == cls .__type_binary :
439
442
payload = dict ['payload' ]
443
+ charset = None
440
444
if isinstance (payload , str ):
441
445
with open (payload , mode = 'rb' ) as file :
442
446
payload = file .read ()
@@ -446,9 +450,10 @@ def parse(cls, dict):
446
450
payload = bytes ()
447
451
elif type == cls .__type_multipart :
448
452
payload = []
453
+ charset = None
449
454
for multipart in dict ['payload' ]:
450
455
payload .append (HttpMultipartBody (multipart ))
451
- return cls (type , payload )
456
+ return cls (type , payload , charset )
452
457
453
458
def __repr__ (self ):
454
459
if self .isMultipart :
@@ -611,9 +616,15 @@ def serialize(self):
611
616
payload = None
612
617
type = HttpBody .__type_none
613
618
else :
614
- payload = self ._payload
619
+ payload = {
620
+ 'text' : self ._payload ,
621
+ 'charset' : self ._charset
622
+ }
615
623
else :
616
- payload = json .dumps (self ._payload )
624
+ payload = {
625
+ 'text' : json .dumps (self ._payload ),
626
+ 'charset' : self ._charset
627
+ }
617
628
elif self .isBinary :
618
629
if len (self ._payload ) == 0 :
619
630
type = HttpBody .__type_none
0 commit comments