You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current dsl helper methods do not allow setting a content type via fileFormField()methods, and default to application/octet-stream. Also the constructor for MultiPartFormField is private. We have scenarios where we need to set the content type for specific MultiPart fields (such as text/csv or text/tab-separated-values).
I was able to work around this using reflection to access the private constructor.
// access private ctor since we need to set file content typevarfileBytes = Files.readAllBytes(Path.of(fileName));
Constructor<MultiPartFormField> multiPartFormFieldConstructor = MultiPartFormField.class.getDeclaredConstructor(String.class, String.class, byte[].class, String.class);
multiPartFormFieldConstructor.setAccessible(true);
MultiPartFormFieldmultiPartFormField = multiPartFormFieldConstructor.newInstance("file", "text/tab-separated-values", fileBytes, fileName);
varformData = http.formData(multiPartFormField);
There are several solutions I can think of to deal with this.
Add new fileFormField helper methods with String contentType argument.
Make the constructor public. It might be difficult to handle all of the different combinations of possible arguments. If the constructor was public you could always fall back on that.
If you don't want a public constructor, then add a single helper method that takes all 4 of the MultiPartFormField values.
Make the constructor protected. I could have created class that extends MultiPartFormField which would have been cleaner than using reflection to change access level of the constructor
The easiest solution might be to just make the constructor public, but the cleanest might be to add new methods to the dsl. One issue is there are already numerous methods that accept multiple String arguments, but the following two methods would allow for a fairly simple fileFormField as well as binaryFormField that would also satisfy option 3 above.
The current dsl helper methods do not allow setting a content type via
fileFormField()
methods, and default toapplication/octet-stream
. Also the constructor for MultiPartFormField is private. We have scenarios where we need to set the content type for specific MultiPart fields (such astext/csv
ortext/tab-separated-values
).I was able to work around this using reflection to access the private constructor.
There are several solutions I can think of to deal with this.
String contentType
argument.MultiPartFormField
values.MultiPartFormField
which would have been cleaner than using reflection to change access level of the constructorThe easiest solution might be to just make the constructor public, but the cleanest might be to add new methods to the dsl. One issue is there are already numerous methods that accept multiple String arguments, but the following two methods would allow for a fairly simple fileFormField as well as binaryFormField that would also satisfy option 3 above.
The text was updated successfully, but these errors were encountered: