vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 536 forks source link

Set<FileUpload> is empty when I used HttpClientRequest forward file uploading request. #1137

Closed silentbalanceyh closed 3 years ago

silentbalanceyh commented 5 years ago

Version

Context

I met an issue when I write following code here, there are two projects:

Testing uri is:

http://localhost:6100/api/attachment/upload/hotel
http://localhost:6201/api/attachment/upload/hotel

I refer upload coding demo: https://github.com/vert-x/vertx-examples/blob/master/src/raw/java/upload/UploadClient.java

When I send request with postman with attachment, here are the http headers came from postman

[ ZERO ] Found remote host: 10.0.0.7, port: 6201, uri: /api/attachment/upload/hotel
[ ZERO ] key = cache-control, value = no-cache
[ ZERO ] Normalized headers: key = Postman-Token, value = 3bd8edf5-42ff-4935-be5e-77fa1c3620a9
[ ZERO ] Normalized headers: key = Accept, value = */*
[ ZERO ] Normalized headers: key = Host, value = localhost:6100
[ ZERO ] Normalized headers: key = cookie, value = access_token= ... ( Ignored )
[ ZERO ] Normalized headers: key = accept-encoding, value = gzip, deflate
[ ZERO ] Normalized headers: key = content-type, value = multipart/form-data; boundary=--------------------------255644102317003986082798
[ ZERO ] Normalized headers: key = content-length, value = 46319
[ ZERO ] Normalized headers: key = Connection, value = keep-alive
[ ZERO ] Normalized headers: key = user-agent, value = Zero Internal Client Agent
final Set<FileUpload> fileUploads = this.context.fileUploads();

Do you have a reproducer?

You can download the project and start two projects to reproduce.

Steps to reproduce

  1. Run the shell under root folder: start-etcd3.sh
  2. Run the main application of vertx-zeus/up-atlas, up.god.Anchor first
  3. Run the api gateway of vertx-zeus/up-athena, up.god.Anchor then
  4. Now you can testing with postman

You can see the log as following: it means that it's running:

Service:

    [ Up Micro ] <Application Name> = "zero-istio",
    [ Up Micro ] Configuration Path = /zero/zero-istio/endpoint/routes/up-atlas:198.18.2.144:6201, 
    [ Up Micro ] Service Name = up-atlas,
    [ Up Micro ] EndPoint = http://198.18.2.144:6201
    [ Up Micro ] Route Uris = 
    [ Up Micro ]    /api/attachment/download/:key
    [ Up Micro ]    /api/attachment/test
    [ Up Micro ]    /api/attachment/upload/:category
    [ Up Micro ]    /api/fix/envelop
    [ Up Micro ]    /api/ipc/future/:name
    [ Up Micro ]    /api/ipc/multi/:name
    [ Up Micro ]    /api/rpc/:name
    [ Up Micro ] √ Successfully to registered Routes, wait for discovery......SUCCESS √

Gateway:

[ ZERO ] ( Api Gateway ) ZeroApiAgent has been started successfully. Endpoint: http://0.0.0.0:6100/.

Extra

I tested with HttpClient/WebClient, all these client will let the Set to empty, but when I used postman send the request directly, it's working. It means that the service should be fine. It also prove the lightweight framework should be working. But when I used the code from:

https://github.com/vert-x/vertx-examples/blob/master/src/raw/java/upload/UploadClient.java

I can see the set size is 0, but request also is multipart:

request.isExpectMultipart() // true

Actually, if I set Api Gateway act file uploading server, this issue could be ignored here, but I think there should be a method to tell me that the HttpClient/WebClient could do this kind of file uploading. Could someone help to resolve this issue ?

The expected situation is that:

When I send request to api gateway, api gateway could forwarded our file uploading request to other micro service, at least other micro service should get the FileUpload object.

silentbalanceyh commented 5 years ago

Append:

http://localhost:6201/api/attachment/upload/hotel ( Working ) http://localhost:6100/api/attachment/upload/hotel ( Issue position )

When I send request to 6201, I can get attachment information as following:

{
  "key" : "7941105d-b041-44f4-81ab-9c22592f5e57",
  "storeWay" : "FILE",
  "status" : "PROGRESS",
  "name" : "《美丽的黄昏》定稿 2.doc",
  "fileKey" : "S681B1SF7mrAjtMRSx5tfiHIuEAfImlLzqcz09H8ZFnkMrQXUsGiCjdF0N7IWl9o",
  "fileName" : "《美丽的黄昏》定稿 2",
  "fileUrl" : "/api/attachment/download/7941105d-b041-44f4-81ab-9c22592f5e57",
  "filePath" : "file-uploads/5bc90d20-8fba-4892-ab70-6139fad83dff",
  "extension" : "doc",
  "module" : "hotel",
  "mime" : "application/msword",
  "size" : 46080,
  "language" : "cn",
  "metadata" : "{}"
}

But when the same request send to Api Gateway ( 6100 ) and forward to 6201, this issue reproduce.

silentbalanceyh commented 5 years ago

I think following code should be helpful, I send request with Apache Http Client, it also works:

        final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
        client.start();
        final File file = new File("data-test");
        final HttpPost post = new HttpPost(
                    "http://localhost:6201/api/attachment/upload/hotel");
        final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);

        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("file", fileBody);
        final HttpEntity entity = builder.build();
        post.setEntity(entity);
        final Future<HttpResponse> response = client.execute(post, null);
        final HttpResponse data = response.get();

But I could not find MultipartEntityBuilder code in vert.x HttpClient/WebClient here, could some one provide helps to me here ?

silentbalanceyh commented 5 years ago

I resolved this issue in another way, I used http client internally instead of WebClient/HttpClient, but I don't know whether this solution is standard here ? Now I can get the data from FileUpload.

vietj commented 5 years ago

can you provide a failing test in this project in the HttpTest class ?

nitishgoyal13 commented 4 years ago

@silentbalanceyh I am facing the same issue. Possible for you to share the repository link where you resolved this issue. Been stuck on this for more than a week now, not able to figure out what is wrong in WebClient of vertx

tsegismont commented 3 years ago

Closing, if you can provide a simple reproducer (a few classes) then feel free to reopen.