Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 8de6725

Browse files
committed
Add response format option #122
1 parent d22c5c9 commit 8de6725

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -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) {

src/ios/RNFetchBlobNetwork.m

+33-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
NSMutableDictionary * progressTable;
2828
NSMutableDictionary * uploadProgressTable;
2929

30+
typedef NS_ENUM(NSUInteger, ResponseFormat) {
31+
UTF8,
32+
BASE64,
33+
AUTO
34+
};
35+
3036

3137
@interface RNFetchBlobNetwork ()
3238
{
@@ -37,6 +43,7 @@ @interface RNFetchBlobNetwork ()
3743
NSMutableDictionary * respInfo;
3844
NSInteger respStatus;
3945
NSMutableArray * redirects;
46+
ResponseFormat responseFormat;
4047
}
4148

4249
@end
@@ -128,6 +135,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
128135
self.options = options;
129136
redirects = [[NSMutableArray alloc] init];
130137
[redirects addObject:req.URL.absoluteString];
138+
139+
// set response format
140+
NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
141+
if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
142+
responseFormat = BASE64;
143+
else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
144+
responseFormat = UTF8;
145+
else
146+
responseFormat = AUTO;
131147

132148
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
133149
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
@@ -357,18 +373,30 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
357373
// if it turns out not to be `nil` that means the response data contains valid UTF8 string,
358374
// in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
359375
NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
360-
361-
if(utf8 != nil)
376+
377+
if(responseFormat == BASE64)
378+
{
379+
rnfbRespType = RESP_TYPE_BASE64;
380+
respStr = [respData base64EncodedStringWithOptions:0];
381+
}
382+
else if (responseFormat == UTF8)
362383
{
363384
rnfbRespType = RESP_TYPE_UTF8;
364385
respStr = utf8;
365386
}
366387
else
367388
{
368-
rnfbRespType = RESP_TYPE_BASE64;
369-
respStr = [respData base64EncodedStringWithOptions:0];
389+
if(utf8 != nil)
390+
{
391+
rnfbRespType = RESP_TYPE_UTF8;
392+
respStr = utf8;
393+
}
394+
else
395+
{
396+
rnfbRespType = RESP_TYPE_BASE64;
397+
respStr = [respData base64EncodedStringWithOptions:0];
398+
}
370399
}
371-
372400
}
373401
}
374402

0 commit comments

Comments
 (0)