Skip to content

Commit 82d5cbf

Browse files
author
Caitlin Bales (MSFT)
committed
Return response headers on responses with no body
1 parent 8268b26 commit 82d5cbf

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.io.BufferedInputStream;
3737
import java.io.BufferedOutputStream;
38+
import java.io.ByteArrayInputStream;
3839
import java.io.IOException;
3940
import java.io.InputStream;
4041
import java.io.OutputStream;
@@ -297,13 +298,13 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
297298

298299
if (connection.getResponseCode() == HttpResponseCode.HTTP_NOBODY
299300
|| connection.getResponseCode() == HttpResponseCode.HTTP_NOT_MODIFIED) {
300-
logger.logDebug("Handling response with no body");
301-
return null;
301+
logger.logDebug("Handling response with no body");
302+
return handleEmptyResponse(connection.getResponseHeaders(), resultClass);
302303
}
303304

304305
if (connection.getResponseCode() == HttpResponseCode.HTTP_ACCEPTED) {
305306
logger.logDebug("Handling accepted response");
306-
return null;
307+
return handleEmptyResponse(connection.getResponseHeaders(), resultClass);
307308
}
308309

309310
in = new BufferedInputStream(connection.getInputStream());
@@ -372,8 +373,9 @@ private InputStream handleBinaryStream(final InputStream in) {
372373
/**
373374
* Handles the cause where the response is a JSON object.
374375
*
375-
* @param in The input stream from the response.
376-
* @param clazz The class of the response object.
376+
* @param in The input stream from the response.
377+
* @param responseHeaders The response headers
378+
* @param clazz The class of the response object.
377379
* @param <Result> The type of the response object.
378380
* @return The JSON object.
379381
*/
@@ -385,6 +387,20 @@ private <Result> Result handleJsonResponse(final InputStream in, Map<String, Lis
385387
final String rawJson = streamToString(in);
386388
return getSerializer().deserializeObject(rawJson, clazz, responseHeaders);
387389
}
390+
391+
/**
392+
* Handles the case where the response body is empty.
393+
*
394+
* @param responseHeaders The response headers
395+
* @param clazz The type of the response object
396+
* @return The JSON object
397+
*/
398+
private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHeaders, final Class<Result> clazz) {
399+
//Create an empty object to attach the response headers to
400+
InputStream in = new ByteArrayInputStream("{}".getBytes());
401+
402+
return handleJsonResponse(in, responseHeaders, clazz);
403+
}
388404

389405
/**
390406
* Sets the connection factory for this provider.

0 commit comments

Comments
 (0)