From d2f0b518fbe81fa60949f6791e30c1478c7e7855 Mon Sep 17 00:00:00 2001 From: Arlan Jaska Date: Sat, 2 May 2020 11:22:07 -0700 Subject: [PATCH 1/2] Fix: URLSearchParams dropped when body of XHR --- Libraries/Network/RCTNetworking.mm | 6 ++++++ Libraries/Network/convertRequestBody.js | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index b857c43a9086e4..713fe46f73e00f 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -347,6 +347,8 @@ - (BOOL)canHandleRequest:(NSURLRequest *)request * * - {"blob": {...}}: an object representing a blob * + * - {"urlSearchParams": "..."}: a JS string that is the serialized query string of a urlSearchParams object + * * If successful, the callback be called with a result dictionary containing the following (optional) keys: * * - @"body" (NSData): the body of the request @@ -380,6 +382,10 @@ - (RCTURLRequestCancellationBlock)processDataForHTTPQuery:(nullable NSDictionary NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; return callback(nil, @{@"body": data}); } + NSString *urlSearchParamsString = [RCTConvert NSString:query[@"urlSearchParams"]]; + if (urlSearchParamsString) { + return callback(nil, @{@"body": urlSearchParamsString, @"contentType": @"application/x-www-form-urlencoded"}); + } NSURLRequest *request = [RCTConvert NSURLRequest:query[@"uri"]]; if (request) { diff --git a/Libraries/Network/convertRequestBody.js b/Libraries/Network/convertRequestBody.js index 0db3ed709778b8..9bfdeeb09ec597 100644 --- a/Libraries/Network/convertRequestBody.js +++ b/Libraries/Network/convertRequestBody.js @@ -22,6 +22,7 @@ export type RequestBody = | {uri: string, ...} | ArrayBuffer | $ArrayBufferView; + | URLSearchParams function convertRequestBody(body: RequestBody): Object { if (typeof body === 'string') { @@ -37,6 +38,9 @@ function convertRequestBody(body: RequestBody): Object { // $FlowFixMe: no way to assert that 'body' is indeed an ArrayBufferView return {base64: binaryToBase64(body)}; } + if (body instanceof URLSearchParams) { + return {urlSearchParams: body.toString()}; + } return body; } From 99f7097c21309501b4158efe0f30a69fcea9c589 Mon Sep 17 00:00:00 2001 From: Arlan Jaska Date: Sat, 2 May 2020 11:35:55 -0700 Subject: [PATCH 2/2] Move semi-colon to correct location I don't have my editor set-up for Flow, oops --- Libraries/Network/convertRequestBody.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Network/convertRequestBody.js b/Libraries/Network/convertRequestBody.js index 9bfdeeb09ec597..8fa10544e5780e 100644 --- a/Libraries/Network/convertRequestBody.js +++ b/Libraries/Network/convertRequestBody.js @@ -21,8 +21,8 @@ export type RequestBody = | FormData | {uri: string, ...} | ArrayBuffer - | $ArrayBufferView; - | URLSearchParams + | $ArrayBufferView + | URLSearchParams; function convertRequestBody(body: RequestBody): Object { if (typeof body === 'string') {