@@ -63,6 +63,12 @@ enum ResponseType {
63
63
FileStorage
64
64
}
65
65
66
+ enum ResponseFormat {
67
+ Auto ,
68
+ UTF8 ,
69
+ BASE64
70
+ }
71
+
66
72
public static HashMap <String , Call > taskTable = new HashMap <>();
67
73
static HashMap <String , Boolean > progressReport = new HashMap <>();
68
74
static HashMap <String , Boolean > uploadProgressReport = new HashMap <>();
@@ -83,8 +89,10 @@ enum ResponseType {
83
89
RNFetchBlobBody requestBody ;
84
90
RequestType requestType ;
85
91
ResponseType responseType ;
92
+ ResponseFormat responseFormat = ResponseFormat .Auto ;
86
93
WritableMap respInfo ;
87
94
boolean timeout = false ;
95
+
88
96
ArrayList <String > redirects = new ArrayList <>();
89
97
90
98
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)
200
208
while (it .hasNextKey ()) {
201
209
String key = it .nextKey ();
202
210
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
+ }
205
221
}
206
222
}
207
223
@@ -437,6 +453,10 @@ private void done(Response resp) {
437
453
// string correctly, we should do URL encoding before BASE64.
438
454
byte [] b = resp .body ().bytes ();
439
455
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
+ }
440
460
try {
441
461
encoder .encode (ByteBuffer .wrap (b ).asCharBuffer ());
442
462
// if the data contains invalid characters the following lines will be
@@ -447,7 +467,12 @@ private void done(Response resp) {
447
467
// This usually mean the data is contains invalid unicode characters, it's
448
468
// binary data
449
469
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
+ }
451
476
}
452
477
}
453
478
} catch (IOException e ) {
0 commit comments