fix POST/PUT/PATCH has no body redbox, when xhr is used without body

Summary:
… by passing a empty body
fix #3371
referring to https://github.com/square/okhttp/pull/1559/files
Closes https://github.com/facebook/react-native/pull/4518

Reviewed By: svcscm

Differential Revision: D2753086

Pulled By: lexs

fb-gh-sync-id: 5c486b127b194b29cd0f8a2cb9a1ef19449109e3
This commit is contained in:
Qiao Liang 2015-12-12 09:09:09 -08:00 committed by facebook-github-bot-9
parent c2b38c9113
commit c60b581327
2 changed files with 13 additions and 1 deletions

View File

@ -136,7 +136,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
requestBuilder.headers(requestHeaders);
if (data == null) {
requestBuilder.method(method, null);
requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method));
} else if (data.hasKey(REQUEST_BODY_KEY_STRING)) {
if (contentType == null) {
onRequestError(requestId, "Payload is set but no content-type header specified");

View File

@ -27,6 +27,7 @@ import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.internal.Util;
import okio.BufferedSink;
import okio.ByteString;
import okio.Okio;
import okio.Source;
@ -112,4 +113,15 @@ import okio.Source;
}
};
}
/**
* Creates a empty RequestBody if required by the http method spec, otherwise use null
*/
public static RequestBody getEmptyBody(String method) {
if (method.equals("POST") || method.equals("PUT") || method.equals("PATCH")) {
return RequestBody.create(null, ByteString.EMPTY);
} else {
return null;
}
}
}