Skip to content

Commit

Permalink
fix: fixed issues with creating output stream while making post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Desu Sai Venkat committed Jul 18, 2023
1 parent 4e0f518 commit d7e2ed9
Showing 1 changed file with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,35 @@ HttpURLConnection updateHttpConnection(HttpURLConnection httpConnection, Request
String requestPayload, boolean isDMTRequest,
@Nullable Map<String, String> customRequestHeaders,
@Nullable FunctionUtils.Function<OutputStream, OutputStream> connectionWrapperOSGenerator) {
try (OutputStream os = httpConnection.getOutputStream();
OutputStream oss = connectionWrapperOSGenerator != null ? connectionWrapperOSGenerator.apply(os) : null;
OutputStreamWriter osw = new OutputStreamWriter(oss != null ? oss : os, StandardCharsets.UTF_8);
) {
httpConnection.setRequestProperty("Authorization", String.format(Locale.US, "Basic %s", authHeaderString));
if (requestMethod == RequestMethod.GET) {

httpConnection.setRequestProperty("Authorization", String.format(Locale.US, "Basic %s", authHeaderString));
if (requestMethod == RequestMethod.GET) {
try {
httpConnection.setRequestMethod("GET");
} else {
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
httpConnection.setRequestProperty("AnonymousId", anonymousIdHeaderString);
if (customRequestHeaders != null && !customRequestHeaders.isEmpty()) {
for (Map.Entry<String, String> entry : customRequestHeaders.entrySet()) {
httpConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
String requestPayloadWithMetadata = withAddedMetadataToRequestPayload(requestPayload, isDMTRequest);
osw.write(requestPayloadWithMetadata);
osw.flush();
} catch (Exception ex) {
RudderLogger.logError("RudderNetworkManager: updateHttpConnection: Error while updating the http connection" + ex.getLocalizedMessage());
return null;
}
return httpConnection;
}
// if the request is of type POST
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Type", "application/json");
httpConnection.setRequestProperty("AnonymousId", anonymousIdHeaderString);
if (customRequestHeaders != null && !customRequestHeaders.isEmpty()) {
for (Map.Entry<String, String> entry : customRequestHeaders.entrySet()) {
httpConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
try (
OutputStream os = httpConnection.getOutputStream();
OutputStream oss = connectionWrapperOSGenerator != null ? connectionWrapperOSGenerator.apply(os) : null;
OutputStreamWriter osw = new OutputStreamWriter(oss != null ? oss : os, StandardCharsets.UTF_8);
) {
httpConnection.setRequestMethod("POST");
String requestPayloadWithMetadata = withAddedMetadataToRequestPayload(requestPayload, isDMTRequest);
osw.write(requestPayloadWithMetadata);
osw.flush();
return httpConnection;
} catch (Exception ex) {
RudderLogger.logError("RudderNetworkManager: updateHttpConnection: Error while updating the http connection" + ex.getLocalizedMessage());
Expand Down

0 comments on commit d7e2ed9

Please sign in to comment.