Skip to content

Commit 9304df3

Browse files
Adam ComellaFacebook Github Bot 6
Adam Comella
authored and
Facebook Github Bot 6
committed
For file uploads, don't stomp on developer's Content-Type header
Summary: Currently when doing a file upload, the Content-Type header gets set to whatever MIME type iOS computed for the file. The Content-Type header the developer provided never takes precedence. For example, when uploading an image, iOS might determine that the MIME type is "image/jpeg" and so this would be the Content-Type of the HTTP request. But the developer might need the Content-Type to be "application/octet-stream". With this change, if the developer provides a Content-Type header, it will not be overriden. There is only one exception to this rule which is for "multipart" requests. In this case, the developer's Content-Type header is always ignored. This is because the Content-Type header needs to contain the boundary string and that information is not available to the developer in JavaScript. This change makes iOS's behavior more consistent with Android's. **Test plan (required)** In a small test app, verified that the developer's Content-Type header takes precedence when it's provided. Verif Closes #9651 Differential Revision: D3820001 Pulled By: mkonicek fbshipit-source-id: fdb8871f88a0d0db1ae59f75bb62b896fe69542d
1 parent 81864e1 commit 9304df3

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Libraries/Network/RCTNetworking.mm

+8-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,14 @@ - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary<NSString *, id> *)q
243243
return (RCTURLRequestCancellationBlock)nil;
244244
}
245245
request.HTTPBody = result[@"body"];
246-
NSString *contentType = result[@"contentType"];
247-
if (contentType) {
248-
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
246+
NSString *dataContentType = result[@"contentType"];
247+
NSString *requestContentType = [request valueForHTTPHeaderField:@"Content-Type"];
248+
BOOL isMultipart = [dataContentType hasPrefix:@"multipart"];
249+
250+
// For multipart requests we need to override caller-specified content type with one
251+
// from the data object, because it contains the boundary string
252+
if (dataContentType && ([requestContentType length] == 0 || isMultipart)) {
253+
[request setValue:dataContentType forHTTPHeaderField:@"Content-Type"];
249254
}
250255

251256
// Gzip the request body

0 commit comments

Comments
 (0)