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
|
MultipartFile imageFile
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// URL absolue
|
// Construire l’URL 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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user