Skip to content

Commit 1eeea9e

Browse files
committed
update
1 parent bbac1b6 commit 1eeea9e

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.hc.client5.http.classic.methods.HttpPost;
3434
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
3535
import org.apache.hc.core5.http.ClassicHttpResponse;
36+
import org.apache.hc.core5.http.ContentType;
3637
import org.apache.hc.core5.http.Header;
3738
import org.apache.hc.core5.http.io.entity.StringEntity;
3839
import org.apache.hc.core5.http.message.BasicHeader;
@@ -94,7 +95,7 @@ public <T extends RESTResponse> T post(
9495
HttpPost httpPost = new HttpPost(getRequestUrl(path, null));
9596
String encodedBody = RESTUtil.encodedBody(body);
9697
if (encodedBody != null) {
97-
httpPost.setEntity(new StringEntity(encodedBody));
98+
httpPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_JSON));
9899
}
99100
Header[] authHeaders = getHeaders(path, "POST", encodedBody, restAuthFunction);
100101
httpPost.setHeaders(authHeaders);
@@ -112,7 +113,7 @@ public <T extends RESTResponse> T delete(
112113
HttpDelete httpDelete = new HttpDelete(getRequestUrl(path, null));
113114
String encodedBody = RESTUtil.encodedBody(body);
114115
if (encodedBody != null) {
115-
httpDelete.setEntity(new StringEntity(encodedBody));
116+
httpDelete.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_JSON));
116117
}
117118
Header[] authHeaders = getHeaders(path, "DELETE", encodedBody, restAuthFunction);
118119
httpDelete.setHeaders(authHeaders);

paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.hc.client5.http.classic.methods.HttpPost;
2525
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
2626
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
27+
import org.apache.hc.core5.http.ContentType;
2728
import org.apache.hc.core5.http.Header;
2829
import org.apache.hc.core5.http.io.entity.StringEntity;
2930
import org.apache.hc.core5.http.message.BasicHeader;
@@ -55,7 +56,11 @@ public String post(String url, Object body, Map<String, String> headers) throws
5556
}
5657
String encodedBody = RESTUtil.encodedBody(body);
5758
if (encodedBody != null) {
58-
httpPost.setEntity(new StringEntity(encodedBody));
59+
ContentType contentType =
60+
body instanceof Map
61+
? ContentType.APPLICATION_FORM_URLENCODED
62+
: ContentType.APPLICATION_JSON;
63+
httpPost.setEntity(new StringEntity(encodedBody, contentType));
5964
}
6065

6166
return exec(httpPost);

paimon-core/src/test/java/org/apache/paimon/rest/HttpClientTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

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

30+
import okhttp3.mockwebserver.RecordedRequest;
3031
import org.junit.After;
3132
import org.junit.Before;
3233
import org.junit.Test;
@@ -37,6 +38,7 @@
3738
import java.util.Collections;
3839
import java.util.HashMap;
3940
import java.util.Map;
41+
import java.util.concurrent.TimeUnit;
4042
import java.util.stream.Collectors;
4143

4244
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -261,4 +263,16 @@ public void testPostWithNonJsonErrorResponse() {
261263
"Error message should contain the original non-JSON response");
262264
}
263265
}
266+
267+
@Test
268+
public void testPostSetsJsonContentType() throws Exception {
269+
server.enqueueResponse(mockResponseDataStr, 200);
270+
httpClient.post(MOCK_PATH, mockResponseData, MockRESTData.class, restAuthFunction);
271+
RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
272+
String contentType = request.getHeader("Content-Type");
273+
Assertions.assertNotNull(contentType, "POST request must carry a Content-Type header");
274+
Assertions.assertTrue(
275+
contentType.contains("application/json"),
276+
"POST body must be sent as application/json, but was: " + contentType);
277+
}
264278
}

0 commit comments

Comments
 (0)