diff --git a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java index a603f0e7..709814fd 100644 --- a/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +++ b/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java @@ -638,6 +638,9 @@ private void releaseTaskResource() { */ private void done(Response resp) { boolean isBlobResp = isBlobResponse(resp); + WritableMap respmap = getResponseInfo(resp,isBlobResp); + emitStateEvent(respmap.copy()); + emitStateEvent(getResponseInfo(resp, isBlobResp)); switch (responseType) { case KeepInMemory: @@ -656,7 +659,7 @@ private void done(Response resp) { ins.close(); os.flush(); os.close(); - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest, respmap.copy()); } // response data directly pass to JS context as string. else { @@ -678,11 +681,11 @@ private void done(Response resp) { invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null); return; } - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy()); return; } if (responseFormat == ResponseFormat.BASE64) { - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP)); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy()); return; } try { @@ -699,9 +702,9 @@ private void done(Response resp) { catch (CharacterCodingException ignored) { if (responseFormat == ResponseFormat.UTF8) { String utf8 = new String(b); - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8, respmap.copy()); } else { - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP)); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy()); } } } @@ -746,16 +749,16 @@ private void done(Response resp) { } if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) { - invoke_callback("Download interrupted.", null); + invoke_callback("Download interrupted.", null, respmap.copy()); } else { this.destPath = this.destPath.replace("?append=true", ""); - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy()); } break; default: try { - invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8")); + invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"), respmap.copy()); } catch (IOException e) { invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null); } diff --git a/fetch.js b/fetch.js index 3300337b..cadeb77b 100644 --- a/fetch.js +++ b/fetch.js @@ -163,7 +163,7 @@ export function fetch(...args: any): Promise { let taskId = getUUID(); let options = this || {}; let subscription, subscriptionUpload, stateEvent, partEvent; - let respInfo = {}; + let respInfo = {'uninit': true}; let [method, url, headers, body] = [...args]; // # 241 normalize null or undefined headers, in case nil or null string @@ -242,8 +242,9 @@ export function fetch(...args: any): Promise { * in JS context, and this parameter indicates which one * dose the response data presents. * @param data {string} Response data or its reference. + * @param responseInfo {Object.<>} */ - req(options, taskId, method, url, headers || {}, body, (err, rawType, data) => { + req(options, taskId, method, url, headers || {}, body, (err, rawType, data, responseInfo) => { // task done, remove event listeners subscription.remove(); @@ -269,6 +270,8 @@ export function fetch(...args: any): Promise { fs.session(options.session).add(data); } respInfo.rnfbEncode = rawType; + if (uninit in respInfo && respInfo.uninit) // event didn't fire yet so we override it here + respInfo = responseInfo; resolve(new FetchBlobResponse(taskId, respInfo, data)); } diff --git a/ios/ReactNativeBlobUtilRequest.mm b/ios/ReactNativeBlobUtilRequest.mm index 1b4c3eab..da0bf2bd 100644 --- a/ios/ReactNativeBlobUtilRequest.mm +++ b/ios/ReactNativeBlobUtilRequest.mm @@ -464,11 +464,16 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom } } + NSHTTPURLResponse *response = (NSHTTPURLResponse *) [task response]; callback(@[ errMsg ?: [NSNull null], rnfbRespType ?: @"", - respStr ?: [NSNull null] + respStr ?: [NSNull null], + @{ + @"status": [NSNumber numberWithInteger:[response statusCode]] + } + ]); ]); respData = nil;