Skip to content

Commit 454ab8f

Browse files
Adam Comellafacebook-github-bot
authored andcommitted
BREAKING: iOS: Support withCredentials flag in XHRs
Summary: Corresponding Android PR: #12276 Respect the withCredentials XMLHttpRequest flag for sending cookies with requests. This can reduce payload sizes where large cookies are set for domains. This should fix #5347. This is a breaking change because it alters the default behavior of XHR. Prior to this change, XHR would send cookies by default. After this change, by default, XHR does not send cookies which is consistent with the default behavior of XHR on web for cross-site requests. Developers can restore the previous behavior by passing `true` for XHR's `withCredentials` argument. **Test plan (required)** Verified in a test app that XHR works properly when specifying `withCredentials` as `true`, `false`, and `undefined`. Also, my team uses this change in our app. Adam Comella Microsoft Corp. Closes #12275 Differential Revision: D4673644 Pulled By: mkonicek fbshipit-source-id: 2fd8f536d02fb39d872eb849584c5c4f7e7698c5
1 parent 22b3faf commit 454ab8f

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Libraries/Network/RCTNetworking.ios.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class RCTNetworking extends NativeEventEmitter {
3333
responseType: 'text' | 'base64',
3434
incrementalUpdates: boolean,
3535
timeout: number,
36-
callback: (requestId: number) => any
36+
callback: (requestId: number) => any,
37+
withCredentials: boolean
3738
) {
3839
const body = convertRequestBody(data);
3940
RCTNetworkingNative.sendRequest({
@@ -43,7 +44,8 @@ class RCTNetworking extends NativeEventEmitter {
4344
headers,
4445
responseType,
4546
incrementalUpdates,
46-
timeout
47+
timeout,
48+
withCredentials
4749
}, callback);
4850
}
4951

Libraries/Network/RCTNetworking.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary<NSString *, id> *)q
230230
request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET";
231231
request.allHTTPHeaderFields = [self stripNullsInRequestHeaders:[RCTConvert NSDictionary:query[@"headers"]]];
232232
request.timeoutInterval = [RCTConvert NSTimeInterval:query[@"timeout"]];
233+
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
233234
NSDictionary<NSString *, id> *data = [RCTConvert NSDictionary:RCTNilIfNull(query[@"data"])];
234235
NSString *trackingName = data[@"trackingName"];
235236
if (trackingName) {

Libraries/Network/XMLHttpRequest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
119119
status: number = 0;
120120
timeout: number = 0;
121121
responseURL: ?string;
122+
withCredentials: boolean = false
122123

123124
upload: XMLHttpRequestEventTarget = new XMLHttpRequestEventTarget();
124125

@@ -501,6 +502,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
501502
incrementalEvents,
502503
this.timeout,
503504
this.__didCreateRequest.bind(this),
505+
this.withCredentials
504506
);
505507
}
506508

0 commit comments

Comments
 (0)