-
Notifications
You must be signed in to change notification settings - Fork 435
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
Improve multipart features for httpPost and httpUpload #479
Comments
Hi @abelsromero, thank you for your feature request. Linking these issues: #147 #162 #169 #259 #348 #399. I completely agree with this. I think |
I am not sure I explained myself well. I am not talking about uploading more than one file or setting the names, this more about being able to reuse DataPart for"inline" elements to avoid creating temporal files. |
@abelsromero, currentlý I am reworking the upload/download to use generic streams (even before you posted the issue). That should resolve your issue. |
Cool! Thanks a lot, do not hesitate to mention me if you need a beta tester. |
Hi @abelsromero , this is coming! I have the basics down in #486, but after we merge #485 I am breaking it up into multiple smaller PRs, one specifically to solve your issue. It is, now that I know how to do this, a very trivial issue to solve. |
Can't promise, but I'll have a look at it asap this week. |
* Fix download to memory ( #414) * Fix logging a consumed body Just ensures the request can STILL be logged, even after the body is consumed (written to an output). * Fix coroutines and remove redundant charset parameter * Fix empty issues When there was not last item, this call would fill. LastOrNull fixes that but this should only return on null items. * Add generic readers and writers for everything that is a body. * Add a few more headers * Add KDoc for Body interface * Add typealias for progress * Add test for DefaultBody * Add empty test * Add Content-Encoding and Transfer-Encoding tests * Add helper for mocking reflection * Add HttpClientTest * Add test to confirm #464 is possible * Add test for issue (#473) * Add test for issue (#408) * Add test for issue (#364) * Add Body for Response This actually makes sure response bodies are treated like request bodies and not read multiple times. This also makes the streams completely lazy (as they are wrapped in an anonymous function). * Add a warning about non-utf8 charset for bodies * Add type aliases * Add lazy length for bodies * Add special "lazy body" and don't call input streams twice This switches to use a UploadBody data class that determines its length at most once, and writes directly to the output stream. This is actually prework for #479 #399 and #390 #344. * Add use caches option to client and request By default, HTTPUrlConnection supports caching, but this was always turned off. This enables it by default, but allows for a "browser refresh" by passing in an argument. * Add TE/Transfer-Encoding to HttpClient * Add explanatory comments to HttpClient * Add request flag to stop decoding content This can be used if for example you want to download some content as gzipped and store it directly. * Change (simplify) non-null invokes * Change classes to data classes for easy copy * Change condition to have common path first * Change body to be an interface This allows us to actually define different type of bodies, as well as extending a body to support #464. The length is now also a lazy call, so that it may be calculated ONLY if necessary. * Change default name of files to "file" The empty filename with _1 looks disturbing. * Change automatic determination of a file's length to only match the encoded length, if it's going to be written with UTF-8. * Change response method types (cleanup) * Change argument naming * Change input stream to be buffered * Change to return instantly if empty * Change default bodies to be consumable at most once This solves multiple issues with input streams being read twice, when logging is turned on. Before, toByteArray would load into memory and that was that. Now it reads directly from an inputstream. However, most streams are only consumable once, thus this solves that. When the stream is read into memory we might as well keep it around. * Change upload body to be writable at most once See the commentary for the DefaultBody * Change the client for the new body api * 📖 Change and format README with new API * Remove redundant braces * Remove bash dollar sign The bash dollar sign only indicates that a string is a command and not a result. It should not be part of the actual string returned, as the string returned can not be forwarded to a system call. * Remove debugging println * Remove debug logging * Remove TODO The logingResponseLogger logged both request and response. Now it correctly only logs the response, and expects you to log the request using a request interceptor. This also makes the logs not show up next to each other, but when they are executed, allowing us to debug async requests. * Remove default ACCEPT_ENCODING Accept-Encoding is to accept content with a certain Content-Encoding, for example when request a file, you may ask for it gziped. However, it is not intended to be used as the encoding of the stream, also known as Transfer-Encoding. * Remove deprecated fields
Just to be sure! It's not merged in yet, nor released 😅 but got it working with an experimental change |
don't worry, that was clear 👍 I'll check the branches |
Based on RFC7578: https://tools.ietf.org/html/rfc7578 This is a breaking change, but mostly deprecates current functions as error. Removes all the extranous methods and bookkeeping from UploadRequest and UploadBody, and delegates those to the DataPart. This also allows for InlineDataPart that yield a file on the server and removes automatic naming for dataparts (_1, _2), because the correct way to put multiple files under the same name is to give them... the same name! Fixes #344 Fixes #390 Fixes #399 Fixes #479 Fixes #513
* Overhaul upload requests, Blob and DataPart Based on RFC7578: https://tools.ietf.org/html/rfc7578 This is a breaking change, but mostly deprecates current functions as error. Removes all the extranous methods and bookkeeping from UploadRequest and UploadBody, and delegates those to the DataPart. This also allows for InlineDataPart that yield a file on the server and removes automatic naming for dataparts (_1, _2), because the correct way to put multiple files under the same name is to give them... the same name! Fixes #344 Fixes #390 Fixes #399 Fixes #479 Fixes #513 * Add kdocs * Tests, docs and rock & roll * Remove debug information * Use new API * Add convenience methods * 💅 Clean up warnings
Feature Request
With the current API, some scenarios of multipart interaction are not friendly and require what imho are hacks.
Description
I am working with CMS API and there's a API call with requires uploading a document alongside metadata in JSON.
This is easily done in curl with:
I only got this to work with the following code
As you can see this requires storing the JSON in a file. That's because I could not find any way to set the Content-Type for the part other than using
DataPart
. Usingparameters
as seen in the commented line sets it astext/plain
causing the service to return an error.I also tried something like
Proposed Solution
Imho, the naming
upload
is confusing for this cases but is very useful for simple 1 file uploads. I'd advocate to:Probably, with only point 2 would be enough to support a cleaner method with httpUpload.
Alternatives I've considered
Current solution does not allow to generate metadata Json on-the fly, which is not valid for a dynamic system.
Also, the
dataParts
closure element seems cumbersome to me and does not help readability. I prefer simpler/cleanerhttpPost
syntax. But this is a more personal thing.Additional context
I noticed that when no Content-Type is set in the DataPart, the header is added empty, maybe this could be just removed.
The text was updated successfully, but these errors were encountered: