Refactor PrestashopClient to improve multipart image upload construction and enhance header management

This commit is contained in:
Vincent Guillet
2025-12-03 14:53:39 +01:00
parent f975e57110
commit 48f7e84ef9

View File

@@ -242,7 +242,7 @@ public class PrestashopClient {
MultipartFile imageFile MultipartFile imageFile
) { ) {
try { try {
// URL absolue // Construire lURL complète
StringBuilder urlBuilder = new StringBuilder(baseUrl) StringBuilder urlBuilder = new StringBuilder(baseUrl)
.append("/images/products/") .append("/images/products/")
.append(productId); .append(productId);
@@ -269,19 +269,21 @@ public class PrestashopClient {
url, bytes.length, contentType url, bytes.length, contentType
); );
// Construction propre du multipart avec un champ `image` fichier // ----- PARTIE FICHIER "image" -----
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); HttpHeaders partHeaders = new HttpHeaders();
bodyBuilder partHeaders.setContentType(MediaType.parseMediaType(contentType));
.part("image", bytes) // Très important : filename + name = "image" pour que Presta le voie dans $_FILES['image']
.filename(filename) partHeaders.setContentDispositionFormData("image", filename);
.contentType(MediaType.parseMediaType(contentType));
MultiValueMap<String, HttpEntity<?>> multipartBody = bodyBuilder.build(); HttpEntity<byte[]> imagePart = new HttpEntity<>(bytes, partHeaders);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("image", imagePart);
return client.post() return client.post()
.uri(URI.create(url)) .uri(URI.create(url))
.contentType(MediaType.MULTIPART_FORM_DATA) .contentType(MediaType.MULTIPART_FORM_DATA)
.body(multipartBody) .body(body)
.retrieve() .retrieve()
.toEntity(String.class); .toEntity(String.class);