Skip to content
Open
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 @@ -33,6 +33,7 @@
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
Expand Down Expand Up @@ -94,7 +95,7 @@ public <T extends RESTResponse> T post(
HttpPost httpPost = new HttpPost(getRequestUrl(path, null));
String encodedBody = RESTUtil.encodedBody(body);
if (encodedBody != null) {
httpPost.setEntity(new StringEntity(encodedBody));
httpPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_JSON));
}
Header[] authHeaders = getHeaders(path, "POST", encodedBody, restAuthFunction);
httpPost.setHeaders(authHeaders);
Expand All @@ -112,7 +113,7 @@ public <T extends RESTResponse> T delete(
HttpDelete httpDelete = new HttpDelete(getRequestUrl(path, null));
String encodedBody = RESTUtil.encodedBody(body);
if (encodedBody != null) {
httpDelete.setEntity(new StringEntity(encodedBody));
httpDelete.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_JSON));
}
Header[] authHeaders = getHeaders(path, "DELETE", encodedBody, restAuthFunction);
httpDelete.setHeaders(authHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
Expand Down Expand Up @@ -55,7 +56,11 @@ public String post(String url, Object body, Map<String, String> headers) throws
}
String encodedBody = RESTUtil.encodedBody(body);
if (encodedBody != null) {
httpPost.setEntity(new StringEntity(encodedBody));
ContentType contentType =
body instanceof Map
? ContentType.APPLICATION_FORM_URLENCODED
: ContentType.APPLICATION_JSON;
httpPost.setEntity(new StringEntity(encodedBody, contentType));
}

return exec(httpPost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.paimon.shade.guava30.com.google.common.collect.ImmutableMap;

import okhttp3.mockwebserver.RecordedRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -37,6 +38,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand Down Expand Up @@ -261,4 +263,16 @@ public void testPostWithNonJsonErrorResponse() {
"Error message should contain the original non-JSON response");
}
}

@Test
public void testPostSetsJsonContentType() throws Exception {
server.enqueueResponse(mockResponseDataStr, 200);
httpClient.post(MOCK_PATH, mockResponseData, MockRESTData.class, restAuthFunction);
RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
String contentType = request.getHeader("Content-Type");
Assertions.assertNotNull(contentType, "POST request must carry a Content-Type header");
Assertions.assertTrue(
contentType.contains("application/json"),
"POST body must be sent as application/json, but was: " + contentType);
}
}