Skip to content

Commit 6e85a3b

Browse files
committed
always set Content-Length header in case of method defines a meaning for an enclosed payload body.
1 parent 387f958 commit 6e85a3b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ private static void addHeaders(HttpURLConnection connection, Map<String, String>
192192
private static void addBody(HttpURLConnection connection, byte[] content, boolean requiresBody) throws IOException {
193193
final int contentLength = content.length;
194194
if (requiresBody || contentLength > 0) {
195-
prepareConnectionForBodyAndGetOutputStream(connection, contentLength).write(content);
195+
final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength);
196+
if (contentLength > 0) {
197+
outputStream.write(content);
198+
}
196199
}
197200
}
198201

@@ -205,9 +208,10 @@ private static void addBody(HttpURLConnection connection, MultipartPayload multi
205208

206209
if (requiresBody) {
207210
final ByteArrayOutputStream os = getPayload(multipartPayload);
208-
209-
if (os.size() > 0) {
210-
os.writeTo(prepareConnectionForBodyAndGetOutputStream(connection, os.size()));
211+
final int contentLength = os.size();
212+
final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength);
213+
if (contentLength > 0) {
214+
os.writeTo(outputStream);
211215
}
212216
}
213217
}
@@ -261,8 +265,11 @@ private static OutputStream prepareConnectionForBodyAndGetOutputStream(HttpURLCo
261265
if (connection.getRequestProperty(CONTENT_TYPE) == null) {
262266
connection.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
263267
}
264-
connection.setDoOutput(true);
265-
return connection.getOutputStream();
268+
if (contentLength > 0) {
269+
connection.setDoOutput(true);
270+
return connection.getOutputStream();
271+
} else {
272+
return null;
273+
}
266274
}
267-
268275
}

0 commit comments

Comments
 (0)