mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
XHR: support typed arrays for request payloads
Summary: Support `xhr.send(data)` for typed arrays. **Test plan:** run UIExplorer example on iOS and Android. Closes https://github.com/facebook/react-native/pull/11904 Differential Revision: D4425551 fbshipit-source-id: 065ab5873407a406ca4a831068ab138606c3361b
This commit is contained in:
committed by
Facebook Github Bot
parent
116916b98d
commit
16bb6e87ba
@@ -46,6 +46,7 @@ import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.ByteString;
|
||||
|
||||
/**
|
||||
* Implements the XMLHttpRequest JavaScript interface.
|
||||
@@ -58,6 +59,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
private static final String REQUEST_BODY_KEY_STRING = "string";
|
||||
private static final String REQUEST_BODY_KEY_URI = "uri";
|
||||
private static final String REQUEST_BODY_KEY_FORMDATA = "formData";
|
||||
private static final String REQUEST_BODY_KEY_BASE64 = "base64";
|
||||
private static final String USER_AGENT_HEADER_NAME = "user-agent";
|
||||
private static final int CHUNK_TIMEOUT_NS = 100 * 1000000; // 100ms
|
||||
private static final int MAX_CHUNK_SIZE_BETWEEN_FLUSHES = 8 * 1024; // 8K
|
||||
@@ -251,6 +253,20 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
} else {
|
||||
requestBuilder.method(method, RequestBody.create(contentMediaType, body));
|
||||
}
|
||||
} else if (data.hasKey(REQUEST_BODY_KEY_BASE64)) {
|
||||
if (contentType == null) {
|
||||
ResponseUtil.onRequestError(
|
||||
eventEmitter,
|
||||
requestId,
|
||||
"Payload is set but no content-type header specified",
|
||||
null);
|
||||
return;
|
||||
}
|
||||
String base64String = data.getString(REQUEST_BODY_KEY_BASE64);
|
||||
MediaType contentMediaType = MediaType.parse(contentType);
|
||||
requestBuilder.method(
|
||||
method,
|
||||
RequestBody.create(contentMediaType, ByteString.decodeBase64(base64String)));
|
||||
} else if (data.hasKey(REQUEST_BODY_KEY_URI)) {
|
||||
if (contentType == null) {
|
||||
ResponseUtil.onRequestError(
|
||||
|
||||
Reference in New Issue
Block a user