Skip to content

Commit f5207ba

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Fix blob response parsing for empty body on iOS
Summary: We currently handle empty body poorly in the iOS blob implementation, this happens because of an early return that cause the blob response to not be processed by the blob module, resulting in an empty string as the body instead of a blob object. We also need to make sure to create an empty blob object when data is nil (empty body) as per the XMLHttpRequest spec. The Android implementation was already handling this properly. Fixes #18223 Send a HEAD request ```js fetch('https://apipre.monkimun.com/whoami', { body: null, method: 'HEAD', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }) ``` [IOS][BUGFIX][Blob] - Fix blob response parsing for empty body Closes #18547 Differential Revision: D7415950 Pulled By: hramos fbshipit-source-id: 56860532c6171255869f02a0960f55d155184a46
1 parent de1040f commit f5207ba

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Libraries/Blob/RCTBlobManager.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ - (BOOL)canHandleNetworkingResponse:(NSString *)responseType
258258

259259
- (id)handleNetworkingResponse:(NSURLResponse *)response data:(NSData *)data
260260
{
261+
// An empty body will have nil for data, in this case we need to return
262+
// an empty blob as per the XMLHttpRequest spec.
263+
data = data ?: [NSData new];
261264
return @{
262265
@"blobId": [self store:data],
263266
@"offset": @0,

Libraries/Network/RCTNetworking.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,6 @@ - (void)sendData:(NSData *)data
439439
{
440440
RCTAssertThread(_methodQueue, @"sendData: must be called on method queue");
441441

442-
if (data.length == 0) {
443-
return;
444-
}
445-
446442
id responseData = nil;
447443
for (id<RCTNetworkingResponseHandler> handler in _responseHandlers) {
448444
if ([handler canHandleNetworkingResponse:responseType]) {
@@ -452,6 +448,10 @@ - (void)sendData:(NSData *)data
452448
}
453449

454450
if (!responseData) {
451+
if (data.length == 0) {
452+
return;
453+
}
454+
455455
if ([responseType isEqualToString:@"text"]) {
456456
// No carry storage is required here because the entire data has been loaded.
457457
responseData = [RCTNetworking decodeTextData:data fromResponse:task.response withCarryData:nil];

0 commit comments

Comments
 (0)