diff --git a/Libraries/Network/RCTNetworking.h b/Libraries/Network/RCTNetworking.h index 2068f32b803038..358ab52b6d1206 100644 --- a/Libraries/Network/RCTNetworking.h +++ b/Libraries/Network/RCTNetworking.h @@ -45,6 +45,8 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request completionBlock:(RCTURLRequestCompletionBlock)completionBlock; +- (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request handler:(id)handler completionBlock:(RCTURLRequestCompletionBlock)completionBlock; + - (void)addRequestHandler:(id)handler; - (void)addResponseHandler:(id)handler; diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index 22ca45b0fa3ff4..7010709e287d93 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -17,6 +17,7 @@ #import #import +#import #import "RCTNetworkPlugins.h" @@ -393,9 +394,9 @@ - (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(nullable NSDictionary } NSURLRequest *request = [RCTConvert NSURLRequest:query[@"uri"]]; if (request) { - __block RCTURLRequestCancellationBlock cancellationBlock = nil; - RCTNetworkTask *task = [self networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { + id handler = [self.bridge moduleForClass:[RCTFileRequestHandler class]]; + RCTNetworkTask *task = [self networkTaskWithRequest:request handler:handler completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) { dispatch_async(self->_methodQueue, ^{ cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil); }); @@ -676,6 +677,19 @@ - (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request completionBlo return task; } +- (RCTNetworkTask *)networkTaskWithRequest:(NSURLRequest *)request handler:(id)handler completionBlock:(RCTURLRequestCompletionBlock)completionBlock +{ + if (!handler) { + // specified handler is nil, fall back to generic method + return [self networkTaskWithRequest:request completionBlock:completionBlock]; + } + RCTNetworkTask *task = [[RCTNetworkTask alloc] initWithRequest:request + handler:handler + callbackQueue:_methodQueue]; + task.completionBlock = completionBlock; + return task; +} + #pragma mark - JS API RCT_EXPORT_METHOD(sendRequest:(JS::NativeNetworkingIOS::SpecSendRequestQuery &)query