-
Notifications
You must be signed in to change notification settings - Fork 278
How to do multipart/mixed upload with Restlet? #1387
cyberquarks
started this conversation in
General
-
Which version of Restlet supports multipart/mixed upload?
The idea is to be able to upload a binary along with JSON in one request.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment