From 1eeea9eac6f4d3679ccb034cb83b16df3e516a0a Mon Sep 17 00:00:00 2001 From: Arnav Balyan Date: Thu, 11 Jun 2026 01:41:19 +0530 Subject: [PATCH] update --- .../java/org/apache/paimon/rest/HttpClient.java | 5 +++-- .../org/apache/paimon/rest/SimpleHttpClient.java | 7 ++++++- .../org/apache/paimon/rest/HttpClientTest.java | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java b/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java index 5b3c481c123b..2477ff9db69b 100644 --- a/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java +++ b/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java @@ -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; @@ -94,7 +95,7 @@ public 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); @@ -112,7 +113,7 @@ public 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); diff --git a/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java b/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java index c2e044d2ccd9..04aaf3bfea64 100644 --- a/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java +++ b/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java @@ -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; @@ -55,7 +56,11 @@ public String post(String url, Object body, Map 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); diff --git a/paimon-core/src/test/java/org/apache/paimon/rest/HttpClientTest.java b/paimon-core/src/test/java/org/apache/paimon/rest/HttpClientTest.java index 6cf9f44802bc..a7424190372d 100644 --- a/paimon-core/src/test/java/org/apache/paimon/rest/HttpClientTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/rest/HttpClientTest.java @@ -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; @@ -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; @@ -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); + } }