@@ -63,6 +63,12 @@ enum ResponseType {
6363 FileStorage
6464 }
6565
66+ enum ResponseFormat {
67+ Auto ,
68+ UTF8 ,
69+ BASE64
70+ }
71+
6672 public static HashMap <String , Call > taskTable = new HashMap <>();
6773 static HashMap <String , Boolean > progressReport = new HashMap <>();
6874 static HashMap <String , Boolean > uploadProgressReport = new HashMap <>();
@@ -83,8 +89,10 @@ enum ResponseType {
8389 RNFetchBlobBody requestBody ;
8490 RequestType requestType ;
8591 ResponseType responseType ;
92+ ResponseFormat responseFormat = ResponseFormat .Auto ;
8693 WritableMap respInfo ;
8794 boolean timeout = false ;
95+
8896 ArrayList <String > redirects = new ArrayList <>();
8997
9098 public RNFetchBlobReq (ReadableMap options , String taskId , String method , String url , ReadableMap headers , String body , ReadableArray arrayBody , final Callback callback ) {
@@ -200,8 +208,16 @@ else if(this.options.fileCache)
200208 while (it .hasNextKey ()) {
201209 String key = it .nextKey ();
202210 String value = headers .getString (key );
203- builder .header (key , value );
204- mheaders .put (key ,value );
211+ if (key .equalsIgnoreCase ("RNFB-Response" )) {
212+ if (value .equalsIgnoreCase ("base64" ))
213+ responseFormat = ResponseFormat .BASE64 ;
214+ else if (value .equalsIgnoreCase ("utf8" ))
215+ responseFormat = ResponseFormat .UTF8 ;
216+ }
217+ else {
218+ builder .header (key , value );
219+ mheaders .put (key , value );
220+ }
205221 }
206222 }
207223
@@ -437,6 +453,10 @@ private void done(Response resp) {
437453 // string correctly, we should do URL encoding before BASE64.
438454 byte [] b = resp .body ().bytes ();
439455 CharsetEncoder encoder = Charset .forName ("UTF-8" ).newEncoder ();
456+ if (responseFormat == ResponseFormat .BASE64 ) {
457+ callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_BASE64 , android .util .Base64 .encodeToString (b , Base64 .NO_WRAP ));
458+ return ;
459+ }
440460 try {
441461 encoder .encode (ByteBuffer .wrap (b ).asCharBuffer ());
442462 // if the data contains invalid characters the following lines will be
@@ -447,7 +467,12 @@ private void done(Response resp) {
447467 // This usually mean the data is contains invalid unicode characters, it's
448468 // binary data
449469 catch (CharacterCodingException ignored ) {
450- callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_BASE64 , android .util .Base64 .encodeToString (b , Base64 .NO_WRAP ));
470+ if (responseFormat == ResponseFormat .UTF8 ) {
471+ callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_UTF8 , "" );
472+ }
473+ else {
474+ callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_BASE64 , android .util .Base64 .encodeToString (b , Base64 .NO_WRAP ));
475+ }
451476 }
452477 }
453478 } catch (IOException e ) {
0 commit comments