Skip to content

fix(api): Use provided Content-Type to create RequestBody #2666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
* Request decorator implementatioon that uses AWS SigV4 signing.
*/
public class IamRequestDecorator implements RequestDecorator {
private static final String CONTENT_TYPE = "application/json";
private static final MediaType JSON_MEDIA_TYPE = MediaType.parse(CONTENT_TYPE);
private static final String DEFAULT_CONTENT_TYPE = "application/json";
private final CredentialsProvider credentialsProvider;
private final AWS4Signer v4Signer;
private final String serviceName;
Expand Down Expand Up @@ -89,8 +88,25 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
//Copy the signed/credentialed request back into an OKHTTP Request object.
okhttp3.Request.Builder okReqBuilder = new okhttp3.Request.Builder();

// By default, we send application/json content-type
String contentType = DEFAULT_CONTENT_TYPE;

for (Map.Entry<String, List<String>> e : request.getHeaders().entries()) {
okReqBuilder.addHeader(e.getKey(), e.getValue().get(0));
String key = e.getKey();
String value = e.getValue().get(0);
okReqBuilder.addHeader(key, value);

// If content-type detected in headers, capture to use later
if ("content-type".equalsIgnoreCase(key)) {
contentType = value;
}
}

// Set MediaType for OkHttp request body, because it will overwrite content-type headers.
MediaType mediaType = MediaType.parse(contentType);
// If the provided content-type is not valid, reset to default MediaType
if (mediaType == null) {
mediaType = MediaType.parse(DEFAULT_CONTENT_TYPE);
}

//Set the URL and Method
Expand All @@ -99,7 +115,7 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
if (req.body() == null) {
requestBody = null;
} else {
requestBody = RequestBody.create(bodyBytes, JSON_MEDIA_TYPE);
requestBody = RequestBody.create(bodyBytes, mediaType);
}

okReqBuilder.method(req.method(), requestBody);
Expand Down

This file was deleted.

Loading