Skip to content

Commit 861468f

Browse files
authored
fix(api): Use provided Content-Type to create RequestBody (#2666)
1 parent 5ae79a2 commit 861468f

File tree

3 files changed

+333
-222
lines changed

3 files changed

+333
-222
lines changed

aws-api/src/main/java/com/amplifyframework/api/aws/auth/IamRequestDecorator.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
* Request decorator implementatioon that uses AWS SigV4 signing.
4141
*/
4242
public class IamRequestDecorator implements RequestDecorator {
43-
private static final String CONTENT_TYPE = "application/json";
44-
private static final MediaType JSON_MEDIA_TYPE = MediaType.parse(CONTENT_TYPE);
43+
private static final String DEFAULT_CONTENT_TYPE = "application/json";
4544
private final CredentialsProvider credentialsProvider;
4645
private final AWS4Signer v4Signer;
4746
private final String serviceName;
@@ -89,8 +88,25 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
8988
//Copy the signed/credentialed request back into an OKHTTP Request object.
9089
okhttp3.Request.Builder okReqBuilder = new okhttp3.Request.Builder();
9190

91+
// By default, we send application/json content-type
92+
String contentType = DEFAULT_CONTENT_TYPE;
93+
9294
for (Map.Entry<String, List<String>> e : request.getHeaders().entries()) {
93-
okReqBuilder.addHeader(e.getKey(), e.getValue().get(0));
95+
String key = e.getKey();
96+
String value = e.getValue().get(0);
97+
okReqBuilder.addHeader(key, value);
98+
99+
// If content-type detected in headers, capture to use later
100+
if ("content-type".equalsIgnoreCase(key)) {
101+
contentType = value;
102+
}
103+
}
104+
105+
// Set MediaType for OkHttp request body, because it will overwrite content-type headers.
106+
MediaType mediaType = MediaType.parse(contentType);
107+
// If the provided content-type is not valid, reset to default MediaType
108+
if (mediaType == null) {
109+
mediaType = MediaType.parse(DEFAULT_CONTENT_TYPE);
94110
}
95111

96112
//Set the URL and Method
@@ -99,7 +115,7 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
99115
if (req.body() == null) {
100116
requestBody = null;
101117
} else {
102-
requestBody = RequestBody.create(bodyBytes, JSON_MEDIA_TYPE);
118+
requestBody = RequestBody.create(bodyBytes, mediaType);
103119
}
104120

105121
okReqBuilder.method(req.method(), requestBody);

aws-api/src/test/java/com/amplifyframework/api/aws/auth/ApiRequestDecoratorFactoryTest.java

-218
This file was deleted.

0 commit comments

Comments
 (0)