Send Multipart/form-data from Vert.x WebClient to Django rest framework

219 Views Asked by At

try to send a document and some data from my vert.x (client-side) to django rest-framework (server-side) : the code server side is :

@api_view(['POST'])
@parser_classes([MultiPartParser, FormParser])
def quick_indexing(request):
    
    data = request.data
    print(data)  # print the entire request data dictionary
    test_value = data.get("test")
    if test_value:
        return Response(f"test attribute value: {test_value}")
    else:
        return Response("test attribute not found")

the code client side :

Vertx vertx = Vertx.vertx();
        WebClient client = WebClient.create(vertx);
        Buffer file = vertx.fileSystem().readFileBlocking("C:/Users/houth/CIN Hassan.pdf");
        MultipartForm form = MultipartForm.create()
            .attribute("test", "succed")
            .binaryFileUpload("file", "filename", file, "Application/pdf");
// Send a POST request with the file and form data
        client.postAbs("http://localhost:8000/quick-indexing")
            .putHeader(String.valueOf(HttpHeaders.CONTENT_TYPE), "multipart/form-data")
            .sendMultipartForm(form,
                ar -> {
                    if (ar.succeeded()) {
                        // Handle the response
                        HttpResponse<Buffer> response = ar.result();
                        System.out.println("Status code: " + response.statusCode());
                        System.out.println("Response body: " + response.bodyAsString());
                    } else {
                        // Handle the failure
                        Throwable cause = ar.cause();
                        System.err.println("Request failed: " + cause.getMessage());
                    }
                });
        System.out.println("end");

---> the result in client side is:

end
Status code: 200
Response body: "test attribute not found"
[ERROR] 2023-04-08 01:39:22.881  invalid version format: <!DOCTYPE
java.lang.IllegalArgumentException: invalid version format: <!DOCTYPE
    at io.netty.handler.codec.http.HttpVersion.<init>(HttpVersion.java:116) ~[netty-codec-http-4.1.86.Final.jar:4.1.86.Final]
    at io.netty.handler.codec.http.HttpVersion.valueOf(HttpVersion.java:78) ~[netty-codec-http-4.1.86.Final.jar:4.1.86.Final]

---> the result in server side is:

<QueryDict: {}>
[08/Apr/2023 01:39:22] "POST /quick-indexing HTTP/1.1" 200 26
[08/Apr/2023 01:39:22] code 400, message Bad request syntax ('9d')
[08/Apr/2023 01:39:22] "9d" 400 -

----> the problem is that if i call the api from postman it succeed

CDL

Send Data with file from vert.x webclient to an api rest framework

1

There are 1 best solutions below

2
med ngh On

Do you ensure that form that you send from vert. X isn't empty