Open dalu93 opened 4 years ago
Hi @dalu93, unfortunately this library's 4.0.0 version is no longer being maintained. It was merged into vapor/vapor a while ago: https://github.com/vapor/vapor/tree/master/Sources/Vapor/Multipart.
Eventually my goal is to split the package back out again and formally pitch this to the swift server work group's package index. Until then, you can access the MultipartParser
and MultipartSerializer
as part of the Vapor
module.
That said, I'm not sure why the length is about 2x longer via MultipartKit here. Seeing the full output (attached as a .txt or something) could help. If this bug exists in the vapor/vapor version of this code I'm happy to fix it.
Hi @tanner0101,
Vapor is not an option for us since we are planning to create a very small CLI tool and importing the whole Vapor product is going to be heavy and useless.
I created, however, an example repository where you can see the error: https://github.com/dalu93/multipartkit-issue
When running through Xcode, set the environment variable FILE_PATH
to your own file path (the file is included in the project as well)
Hi @tanner0101 have you had chance to have a look at it? Unfortunately I cannot run Vapor locally because I'm on MacOS Mojave (10.14) and Vapor only supports 10.15+.
I was anyway able to verify few things:
There is a difference in terms of bytes between the two methods, using MultipartKit
the data is almost 2x the data I get from using my small helper.
So I tried to replicate as much as possible what MultipartKit is doing
multipartData
using my small helperlet multipartData = try MultipartHelper().multipart(
from: fileUrl.path,
partName: "file",
boundary: boundary
)
var buffer = ByteBufferAllocator().buffer(capacity: 0)
buffer.writeBytes(multipartData)
String
let bufferString = String(decoding: buffer.readableBytesView, as: UTF8.self)
multipartData.count
equal to Data(bufferString.utf8).count
print(Data(bufferString.utf8).count == multipartData.count) // got `false`
When I compare the string result of this operation (bufferString
) to the string I get directly using MultipartSerializer
, they are identical
let body = try MultipartSerializer().serialize(
parts: [multipart],
boundary: boundary
)
print(body == bufferString) // got `true`
There are two serialization methods: https://github.com/vapor/multipart-kit/blob/73706f1883f2ba950d41f18aec7e3a53766d4a6d/Sources/MultipartKit/MultipartSerializer.swift#L10-L14 https://github.com/vapor/multipart-kit/blob/73706f1883f2ba950d41f18aec7e3a53766d4a6d/Sources/MultipartKit/MultipartSerializer.swift#L26-L45
The former method you are using uses String
internally so it can corrupt input data.
You have to use the latter.
@dalu93 are you seeing this in the latest release? We've rewritten the parser in Swift so hopefully this issue has been resolved
Hi,
I've been trying using this library (version 4.0.0-beta.2) together with AsyncHTTPClient to execute a multipart POST request, where I need to upload a file.
The expected request should look like the following
This request works well through Insomnia and I tried to convert it to Swift using MultipartKit as below
Unfortunately the data count (1130517) is quite bigger than expected (see Content-Length from Insomnia output).
I've tried my own implementation of this simple case using
Foundation
and I actually am able to create the correct payloadAnd
MultipartHelper.swift
Am I somehow using the library incorrectly?