1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-27 18:21:14 +03:00

Never convert null into an empty request body.

This is a behavior change.
This commit is contained in:
jwilson
2015-04-15 15:49:36 -04:00
parent 4412838406
commit 1abba290f5
5 changed files with 21 additions and 9 deletions

View File

@@ -23,6 +23,7 @@ import com.squareup.okhttp.Headers;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Route;
import com.squareup.okhttp.internal.Internal;
@@ -73,6 +74,7 @@ import okio.Sink;
public class HttpURLConnectionImpl extends HttpURLConnection {
private static final Set<String> METHODS = new LinkedHashSet<>(
Arrays.asList("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "PATCH"));
private static final RequestBody EMPTY_REQUEST_BODY = RequestBody.create(null, new byte[0]);
final OkHttpClient client;
@@ -314,9 +316,13 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
private HttpEngine newHttpEngine(String method, Connection connection,
RetryableSink requestBody, Response priorResponse) {
// OkHttp's Call API requires a placeholder body; the real body will be streamed separately.
RequestBody placeholderBody = HttpMethod.requiresRequestBody(method)
? EMPTY_REQUEST_BODY
: null;
Request.Builder builder = new Request.Builder()
.url(getURL())
.method(method, null /* No body; that's passed separately. */);
.method(method, placeholderBody);
Headers headers = requestHeaders.build();
for (int i = 0, size = headers.size(); i < size; i++) {
builder.addHeader(headers.name(i), headers.value(i));