Skip to content

Commit

Permalink
fix POST/PUT/PATCH has no body redbox, when xhr is used without body
Browse files Browse the repository at this point in the history
Summary:
… by passing a empty body
fix #3371
referring to https://github.com/square/okhttp/pull/1559/files
Closes #4518

Reviewed By: svcscm

Differential Revision: D2753086

Pulled By: lexs

fb-gh-sync-id: 5c486b127b194b29cd0f8a2cb9a1ef19449109e3
  • Loading branch information
qbig authored and facebook-github-bot-9 committed Dec 12, 2015
1 parent c2b38c9 commit c60b581
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void sendRequest(
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.internal.Util;
import okio.BufferedSink;
import okio.ByteString;
import okio.Okio;
import okio.Source;

Expand Down Expand Up @@ -112,4 +113,15 @@ public void writeTo(BufferedSink sink) throws IOException {
}
};
}

/**
* 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;
}
}
}

0 comments on commit c60b581

Please sign in to comment.