Enhance PrestashopClient to use UTF-8 encoding for XML body in POST and PUT methods

This commit is contained in:
Vincent Guillet
2025-12-03 11:39:55 +01:00
parent ce3389f2e6
commit de942e0d96

View File

@@ -19,6 +19,9 @@ import java.util.Base64;
@Slf4j
public class PrestashopClient {
private static final MediaType APPLICATION_XML_UTF8 =
new MediaType("application", "xml", StandardCharsets.UTF_8);
private final RestClient client;
private final String baseUrl;
@@ -73,10 +76,11 @@ public class PrestashopClient {
public String postXml(String path, MultiValueMap<String, String> params, String xmlBody) {
String uri = buildUri(path, params);
log.info("[PrestaShop] POST XML {}", uri);
byte[] bodyBytes = xmlBody.getBytes(StandardCharsets.UTF_8);
return client.post()
.uri(uri)
.contentType(MediaType.APPLICATION_XML)
.body(xmlBody)
.contentType(APPLICATION_XML_UTF8)
.body(bodyBytes)
.retrieve()
.body(String.class);
}
@@ -84,10 +88,11 @@ public class PrestashopClient {
public String putXml(String path, MultiValueMap<String, String> params, String xmlBody) {
String uri = buildUri(path, params);
log.info("[PrestaShop] PUT XML {}", uri);
byte[] bodyBytes = xmlBody.getBytes(StandardCharsets.UTF_8);
return client.put()
.uri(uri)
.contentType(MediaType.APPLICATION_XML)
.body(xmlBody)
.contentType(APPLICATION_XML_UTF8)
.body(bodyBytes)
.retrieve()
.body(String.class);
}
@@ -125,12 +130,15 @@ public class PrestashopClient {
}
log.info("[PrestaShop] POST (proxy) {}", uri);
log.info("[PrestaShop] XML envoyé (proxy POST):\n{}", xmlBody);
byte[] bodyBytes = xmlBody.getBytes(StandardCharsets.UTF_8);
try {
return client.post()
.uri(uri)
.contentType(MediaType.APPLICATION_XML)
.body(xmlBody)
.contentType(APPLICATION_XML_UTF8)
.body(bodyBytes)
.retrieve()
.toEntity(String.class);
} catch (RestClientResponseException ex) {
@@ -157,12 +165,15 @@ public class PrestashopClient {
}
log.info("[PrestaShop] PUT (proxy) {}", uri);
log.info("[PrestaShop] XML envoyé (proxy PUT):\n{}", xmlBody);
byte[] bodyBytes = xmlBody.getBytes(StandardCharsets.UTF_8);
try {
return client.put()
.uri(uri)
.contentType(MediaType.APPLICATION_XML)
.body(xmlBody)
.contentType(APPLICATION_XML_UTF8)
.body(bodyBytes)
.retrieve()
.toEntity(String.class);
} catch (RestClientResponseException ex) {