Ktor MultiPart Form processing is confusing. First...
# ktor
a
Ktor MultiPart Form processing is confusing. First of all, the
PartData
has the following in the comments:
Copy code
/**
 * Represents a multipart/form-data entry. Could be a [FormItem] or [FileItem].
 * @property dispose to be invoked when this part is no longed needed
 * @property headers of this part, could be inaccurate on some engines 
 */
public sealed class PartData(public val dispose: () -> Unit, public val headers: Headers)
• "Property Headers of this part, could be inaccurate": What does this mean? How can it be inaccurate? In which cases? • "Represents a multipart/form-data entry. Could be a [FormItem] or [FileItem].", but there are more subclasses: BinaryChannelItem, BinaryItem. • The dispose method. At which moment should it be used? I only see a reference to it in
installDefaultTransformations
where parameters are added in the Parameters object. What happens if you don't dispose? Do I need to dispose? What happens with files when I dispose? Do I need to dispose in order the fields arive? • In the docs it states: "Ktor allows you to receive form parameters sent with both
x-www-form-urlencoded
and
multipart/form-data
types using the receiveParameters function". So should I use
receiveParameters
or
receiveMultiPart
on a call? Can I receive both?
a
I've created an issue to address the API doc problems.
❤️ 2
s
I'm currently working on processing a MultiPart Form on the server side.
@Aleksei Tirman [JB] I'm currently working on processing a MultiPart Form on the server side. I try to read all requests in a non-blocking manner if possible. It is also not clear to me under which circumstances it is which type of PartData. When is it a FileItem and when is it a BinaryItem or BinaryChannelItem? Or is it currently always a FileItem on the server side? And is there a way to read a FileItem non-blocking? Could we maybe get a small explanation here?
a
On the server, there are only
FileItem
and
FormItem
. The
FileItem
has the
provider
method which can be used to get the
ByteReadChannel
of the item. You can use the channel to read the item's body asynchronously. You can find an example of reading
ByteReadChannel
by chunks in the documentation.