Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: URLSearchParams dropped when body of XHR #28809

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Libraries/Network/RCTNetworking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {

Expand Down
6 changes: 5 additions & 1 deletion Libraries/Network/convertRequestBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type RequestBody =
| FormData
| {uri: string, ...}
| ArrayBuffer
| $ArrayBufferView;
| $ArrayBufferView
| URLSearchParams;

function convertRequestBody(body: RequestBody): Object {
if (typeof body === 'string') {
Expand All @@ -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;
}

Expand Down