react-native/Libraries/Network/RCTDataManager.m
Nick Lockwood a2db4a4a5b Removed redundant JSON encode/decode from RCTDataManager
Summary:
@public

For some reason we were manually JSON-encoding the RCTDataManager responses, and then decoding them again on the JS side. Since all data sent over the bridge is JSON-encoded anyway, this is pretty pointless.

Test Plan:
* Test Movies app in OSS, which uses RCTDataManager
* Test any code that uses RKHTTPQueryGenericExecutor to make network requests (e.g. Groups)
* Test the Groups photo upload feature, which uses RKHTTPQueryWithImageUploadExecutor
2015-06-01 08:35:56 -08:00

83 lines
2.6 KiB
Objective-C

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTDataManager.h"
#import "RCTAssert.h"
#import "RCTConvert.h"
#import "RCTLog.h"
#import "RCTUtils.h"
@implementation RCTDataManager
RCT_EXPORT_MODULE()
/**
* Executes a network request.
* The responseSender block won't be called on same thread as called.
*/
RCT_EXPORT_METHOD(queryData:(NSString *)queryType
withQuery:(NSDictionary *)query
queryHash:(__unused NSString *)queryHash
responseSender:(RCTResponseSenderBlock)responseSender)
{
if ([queryType isEqualToString:@"http"]) {
// Build request
NSURL *URL = [RCTConvert NSURL:query[@"url"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = [RCTConvert NSString:query[@"method"]] ?: @"GET";
request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]];
request.HTTPBody = [RCTConvert NSData:query[@"data"]];
// Build data task
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) {
NSHTTPURLResponse *httpResponse = nil;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
// Might be a local file request
httpResponse = (NSHTTPURLResponse *)response;
}
// Build response
NSArray *responseJSON;
if (connectionError == nil) {
NSStringEncoding encoding = NSUTF8StringEncoding;
if (response.textEncodingName) {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
responseJSON = @[
@(httpResponse.statusCode ?: 200),
httpResponse.allHeaderFields ?: @{},
[[NSString alloc] initWithData:data encoding:encoding] ?: @"",
];
} else {
responseJSON = @[
@(httpResponse.statusCode),
httpResponse.allHeaderFields ?: @{},
connectionError.localizedDescription ?: [NSNull null],
];
}
// Send response (won't be sent on same thread as caller)
responseSender(responseJSON);
}];
[task resume];
} else {
RCTLogError(@"unsupported query type %@", queryType);
}
}
@end