Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(controller): support receiving MultipartFiles #3165

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

cromoteca
Copy link
Contributor

@cromoteca cromoteca commented Jan 17, 2025

Multipart requests are supposed to contain some files (with name) and one JSON part named "hilla_body_part", which contains the ordinary data.

The part names for files must be named according to the JSON syntax for accessing a JSON object. So, for example, if a method has a parameter named uploadedFile, the corresponding path will be /uploadedFile. But paths can also point anywhere in the object, like /orders/1/invoice.

Closes #3130

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.65%. Comparing base (6de563d) to head (b5e645c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3165   +/-   ##
=======================================
  Coverage   92.65%   92.65%           
=======================================
  Files          85       85           
  Lines        3187     3187           
  Branches      778      778           
=======================================
  Hits         2953     2953           
  Misses        182      182           
  Partials       52       52           
Flag Coverage Δ
unittests 92.65% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cromoteca cromoteca changed the title feat: add POC to reattach MultipartFiles to objects POC to reattach MultipartFiles to objects Jan 19, 2025
@cromoteca
Copy link
Contributor Author

To be clear, this also works for top-level parameters:

public String uploadAvatar(String user, MultipartFile file) {
    return String.format("The avatar for %s is %d bytes.\n", user, file.getSize());
}

Request follows:

POST http://localhost:8080/connect/HelloEndpoint/uploadAvatar HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0
Accept: application/json
Accept-Language: en-GB,en;q=0.8,fr-FR;q=0.6,fr;q=0.4,it;q=0.2
Accept-Encoding: gzip, deflate, br, zstd
Content-Type: multipart/form-data;boundary=----MYBOUNDARY
Referer: http://localhost:8080/
X-CSRF-Token: e44c315f-b825-4057-ba5f-2967ed167c71
Origin: http://localhost:8080
DNT: 1
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Connection: keep-alive
Cookie: Idea-ca342cc6=93254400-1659-49ba-9b12-282fb6325c2b; JSESSIONID=04D782331CAE64FB18EED57A754B7661; csrfToken=e44c315f-b825-4057-ba5f-2967ed167c71
Priority: u=0
Pragma: no-cache
Cache-Control: no-cache

------MYBOUNDARY
Content-Disposition: form-data; name="data"
Content-Type: application/json

{
  "user": "John"
}
------MYBOUNDARY
Content-Disposition: form-data; name="/file"; filename="avatar.png"
Content-Type: image/png

iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAdnJLH8AAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZQTFRFAAAA/wAAG/+NIgAAAAF0Uk5TAEDm2GYAAABsSURBVDjLvZNLCgAhDEOT+196GBgY2yYGXJhl+qz9KNDFV/DiJ+GXeCV+jxJYTEqCWil+ATA1yk67M4h4AptbiyHLkglaCgYgdJ4BbgfDMcc+2rEJjJTc6GSZ4c0aALY++6vOAaS4/7oNWY0H7vQCVCaU9zAAAAAASUVORK5CYII=
------MYBOUNDARY--

@cromoteca cromoteca changed the title POC to reattach MultipartFiles to objects feat(controller): support receiving MultipartFiles Jan 27, 2025
@cromoteca cromoteca marked this pull request as ready for review January 27, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File upload support for Hilla endpoint controller
1 participant