Chris Conover
Chris Conover

Reputation: 9039

Alamofire: Support for uploading multipart forms with text and file parts?

Alamofire now has support for uploading files via a multipart form upload.

Looking through the MultipartFormData class API, however, I do not see an obvious way of sending heterogeneous data - textural plain/json data along with the assumed binary file data. In my reading - in the current form, it seems more geared as a multi-file (binary stream) upload without support for text or json parts.

Am I missing something? How does one upload textual/json data parts along with the assumed file data parts? Is there any sample code that does this?

To be complete, I have been writing extensions to fill in some of this, but I keep feeling that there has to be an easier way (my changes require some modification to the Alamofire).

That said, and for what it's worth, here is my attempt at a clean form:

multipartFormData: { form in
    form.appendBodyPart(Multipart.plain("person", text:"\(last.id)")!)
    form.appendBodyPart(Multipart.json("thing", parameters:parameters)!)
    form.appendBodyPart(fileURL: puppyFile, name: "file") },

Obviously a supported solution is better than a re-invented wheel, hence the question.

PS. I am posting this year as per Alamofire documentation, SO is the preferred medium for help and discussion.

Upvotes: 4

Views: 1533

Answers (1)

Chris Conover
Chris Conover

Reputation: 9039

This was not supported at the time of my post, but has been implemented since then, on the multipart_form branch, for the 1.3 release which is now currently available. This related issue is:

https://github.com/Alamofire/Alamofire/pull/596

Upvotes: 3

Related Questions