Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to do multipart/mixed upload with Restlet? #1387

cyberquarks started this conversation in General
Discussion options

Which version of Restlet supports multipart/mixed upload?

The idea is to be able to upload a binary along with JSON in one request.

You must be logged in to vote

Replies: 1 comment

Comment options

I made a test for this:

@Post
public void acceptFormUpload(Representation entity) throws Exception {
 if (entity == null) {
 throw new ResourceException(400); // Bad request
 }
 if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
 FileItemFactory factory = new DiskFileItemFactory();
 RestletFileUpload fileUpload = new RestletFileUpload(factory);
 List<FileItem> fileItems = fileUpload.parseRepresentation(entity);
 if (fileItems == null) {
 throw new ResourceException(400); // Bad request
 }
 for (FileItem fileItem : fileItems) {
 if (fileItem.isFormField()) {
 String fieldName = fileItem.getFieldName();
 String fieldValue = fileItem.getString();
 System.out.println("Field name: " + fieldName + ", Value: " + fieldValue);
 }
 else {
 String fieldName = fileItem.getFieldName();
 String fileName = fileItem.getName();
 long fileSize = fileItem.getSize();
 System.out.println("File size: " + fileSize + " bytes");
 }
 }
 } else {
 throw new ResourceException(415); // Unsupported media type
 }
}

I tried this with:

POST http://localhost:8080
Content-Type: multipart/form-data; boundary=*****BoundaryMarker*****
--*****BoundaryMarker*****
Content-Disposition: form-data; name="file"; filename="2024-03-31T081059.200.json"
Content-Type: application/json

And with:

POST http://localhost:8080
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="textfield"
textvalue
--WebAppBoundary
Content-Disposition: form-data; name="radiobutton"
selectedvalue
--WebAppBoundary
Content-Disposition: form-data; name="checkbox"
true
--WebAppBoundary--

And:

POST http://localhost:8080
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="textfield"
textvalue
--WebAppBoundary
Content-Disposition: form-data; name="radiobutton"
selectedvalue
--WebAppBoundary
Content-Disposition: form-data; name="checkbox"
true
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="2024-03-31T081059.200.json"
Content-Type: application/json
<@2024年03月31日T081059.200.json
--WebAppBoundary--

It works.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /