Spring Boot POST zu PHP API

OnDemand

Top Contributor
Hallo zusammen,

möchte eine externe API anbinden, dort gibts einen Endpoint siehe unten. Die erwartet ein File als Parameter (PHP Fileformat?) Hat jemand ne Idee was ich senden soll? Base64 und URL zum Bild funktioniert nicht. In der Doku steht dazu auch nix

1629276345793.png
 
Danke schau ich mal. An anderen Stellen steht expliziert, wenn was in den body soll, im screenshot steht als Parameter. Doch nicht etwa url Parameter 🥴
 
Soweit so gut. Aber ich bekomme trotzdem eine 415 MediaType not supported. Vermutlich weil da immer noch was nicht korrekt geschickt wird. Hat jemand eine Idee was ich da vergessen hab?

Java:
rateLimiter.acquire();
        String serviceURL = ....;
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Basic " + base64String);
        headers.setAccept(Arrays.asList(new MediaType[]{MediaType.MULTIPART_FORM_DATA}));
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);    
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("upload_product_image", imageFile);
        body.add("filename", fileName);
        body.add(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA);

        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
        ResponseEntity<String> response = restTemplate.exchange(serviceURL, HttpMethod.POST, entity, String.class);


Java:
URI         : https://blabla.com/v2/images
2021-08-19 12:40:48.270 DEBUG 23511 --- [export_Worker-1] .a.u.a.RequestResponseLoggingInterceptor : Method      : POST
2021-08-19 12:40:48.271 DEBUG 23511 --- [export_Worker-1] .a.u.a.RequestResponseLoggingInterceptor : Headers     : [Accept:"multipart/form-data", Authorization:"Basic c3VwcG9ydEBpbXBvcnQyc2hvcC5jb206WGJwdDk0Nyo=", User-Agent:"ddddd", Content-Type:"multipart/form-data;boundary=QoLFGmhtNdv6oXHe0KpWk1mxQOgjGoX6_GS", Content-Length:"723"]
2021-08-19 12:40:48.272 DEBUG 23511 --- [export_Worker-1] .a.u.a.RequestResponseLoggingInterceptor : Request body: --QoLFGmhtNdv6oXHe0KpWk1mxQOgjGoX6_GS
Content-Disposition: form-data; name="upload_product_image"
Content-Type: application/json

"/Users/Downloads/8718475964735_a_en_hd_1.jpg"
--QoLFGmhtNdv6oXHe0KpWk1mxQOgjGoX6_GS
Content-Disposition: form-data; name="filename"
Content-Type: text/plain;charset=UTF-8
Content-Length: 27

8718475964735_a_en_hd_1.jpg
--QoLFGmhtNdv6oXHe0KpWk1mxQOgjGoX6_GS
Content-Disposition: form-data; name="Content-Type"
Content-Type: application/json

{"type":"multipart","subtype":"form-data","parameters":{},"qualityValue":1.0,"wildcardType":false,"wildcardSubtype":false,"subtypeSuffix":null,"charset":null,"concrete":true}
--QoLFGmhtNdv6oXHe0KpWk1mxQOgjGoX6_GS--
 
Zumindest diese Zeile sieht auf den ersten Blick komisch aus, oder erwartest Du wirklich als result auch Multipart Form Data?
Java:
 headers.setAccept(Arrays.asList(new MediaType[]{MediaType.MULTIPART_FORM_DATA}));

Mit setAccept sagst Du ja dem Server was Du akzeptierst. Ich könnte mir durchaus vorstellen, dass der Server Dir aber json oder xml zurück schicken möchte.
 
Stimmt, geht aber trotzdem nicht. Die Doku sagt:
Uploads an image file for the products. Make this request without the "Content-Type: application/json". Except from the file the POST request must also contain a "filename" value with the final filename.
Hilft nur nicht weiter.

Der API Anbieter sagt:
das Bild muss als base64 String direkt als POST übergeben werden und als
Content-Type muss es multipart/form-data sein und das application/json musst
du weglassen.

Das Bild muss als Formularfeld "upload_product_image" gesendet werden.

Hilft mir auch nicht wirklich weiter. In dem Request siehe oben, sehe ich dass da noch was mit application/json steht, wenn ich den Request ausgeben lasse, wie kommt man da ran?
 
So geht es:
Was es da macht? Keine Ahnung aber es geht

Java:
    HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
        ContentDisposition contentDisposition = ContentDisposition
                .builder("form-data")
                .name("filename")
                .filename(fileName)
                .build();
        fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
        byte[] fileContent = new byte[0];
        try {
            fileContent = FileUtils.readFileToByteArray(imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity<byte[]> fileEntity = new HttpEntity<>(fileContent, fileMap);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", fileEntity);
        body.add("filename", fileName);

        HttpEntity<MultiValueMap<String, Object>> requestEntity =
                new HttpEntity<>(body, headers);
 
OK, so habe ich das auch noch nicht gesehen. Aber eines ist mal klar, deren Dokumentation ist nicht ganz korrekt.

Mich würde dennoch interessieren ob das nicht doch funktioniert.
Java:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(imageFile));
body.add("filename", fileName);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
 

Zurück
Oben