Refactor PrestashopClient to improve multipart image upload construction and enhance header management
This commit is contained in:
@@ -242,7 +242,7 @@ public class PrestashopClient {
|
||||
MultipartFile imageFile
|
||||
) {
|
||||
try {
|
||||
// URL absolue
|
||||
// Construire l’URL complète
|
||||
StringBuilder urlBuilder = new StringBuilder(baseUrl)
|
||||
.append("/images/products/")
|
||||
.append(productId);
|
||||
@@ -269,19 +269,21 @@ public class PrestashopClient {
|
||||
url, bytes.length, contentType
|
||||
);
|
||||
|
||||
// Construction propre du multipart avec un champ `image` fichier
|
||||
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
|
||||
bodyBuilder
|
||||
.part("image", bytes)
|
||||
.filename(filename)
|
||||
.contentType(MediaType.parseMediaType(contentType));
|
||||
// ----- PARTIE FICHIER "image" -----
|
||||
HttpHeaders partHeaders = new HttpHeaders();
|
||||
partHeaders.setContentType(MediaType.parseMediaType(contentType));
|
||||
// Très important : filename + name = "image" pour que Presta le voie dans $_FILES['image']
|
||||
partHeaders.setContentDispositionFormData("image", filename);
|
||||
|
||||
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()
|
||||
.uri(URI.create(url))
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(multipartBody)
|
||||
.body(body)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user