0

I have a problem with sending multipart/form-data requests from my Java service (using WebClient but same history with RestTemplate) to Flask service. It seems like a sent body is just empty as I get empty Immutablemultidict([]). I dont think that's revelant but I host java app locally on my machine and Flask app is run in docker container. Does anyone see what can be an issue?

Controller:

@RequestMapping("/api/biometric")
@RequiredArgsConstructor
public class BiometricController {

    private final BiometricClient biometricClient;

    @PostMapping("/register")
    public ResponseEntity<String> registerUser(
            @RequestParam("user_id") String userId,
            @RequestParam("image") MultipartFile image) throws IOException {
        String response = biometricClient.register(userId, image);
        return ResponseEntity.ok(response);
    }
    
}

WebClient:

@Component
public class BiometricClient {

    private final WebClient webClient;

    public BiometricClient() {
        this.webClient = WebClient.builder()
                .baseUrl("http://localhost:5001")
                .build();
    }

    public String register(String userId, MultipartFile image) {
        try {
            Resource file = new org.springframework.core.io.ByteArrayResource(image.getBytes()) {
                @Override
                public String getFilename() {
                    return image.getOriginalFilename();
                }
            };

            MultipartBodyBuilder builder = new MultipartBodyBuilder();
            builder.part("user_id", userId); // string part
            builder.part("image", file);
            MultiValueMap<String, org.springframework.http.HttpEntity<?>> multipartData = builder.build();

            String response = webClient.post()
                    .uri("/register")
                    .contentType(MediaType.MULTIPART_FORM_DATA)
                    .body(BodyInserters.fromMultipartData(multipartData))
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();

            return response;

        } catch (WebClientResponseException e) {
            throw e;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Flask endpoint:

@app.route("/register", methods=["POST"])
def register_user():
    """Receive image, extract embedding, save to DB"""

    # Extract data
    file = request.files.get("image")
    user_id = request.form.get("user_id")

    if not file or not user_id:
        print("❌ Missing image or user_id")
        return jsonify({"error": "Missing image or user_id"}), 400

    # Example: process file
    # embedding = engine.get_embedding(file.read())
    # save_embedding(db_session, user_id, embedding)

    return jsonify({"message": "Embedding registered"}), 200

Curl request to java service:

curl -F "user_id=123" -F "[email protected]" http://localhost:8080/api/biometric/register

Log:

❌ Missing image or user_id
chunked encoding error while discarding request body. client=172.23.0.1 request="POST /register HTTP/1.1" error="invalid literal for int() with base 16: b''"
172.23.0.1 - - [12/Nov/2025 00:14:47] "POST /register HTTP/1.1" 400 159 0.084201

Also, when trying to hardcode filename in webclient service (not pass from controller), it doesnt work or when trying to sent just user_id.

Curl request directly to Flask service:

curl -F "[email protected]" -F "user_id=123" http://localhost:5001/register

It returns then 200.

1
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Nov 12 at 1:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.