@@ -28,6 +28,8 @@ module.exports = function (source, options) {
2828 hasBody : false
2929 }
3030
31+ var indent = opts . indent
32+
3133 // We just want to make sure people understand that is the only dependency
3234 code . push ( 'import Foundation' )
3335
@@ -61,7 +63,34 @@ module.exports = function (source, options) {
6163 break
6264
6365 case 'multipart/form-data' :
64- code . push ( swiftHelpers . multipartBody ( source , opts ) )
66+ /**
67+ * By appending multipart parameters one by one in the resulting snippet,
68+ * we make it easier for the user to edit it according to his or her needs after pasting.
69+ * The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
70+ */
71+ code . push ( swiftHelpers . literalDeclaration ( 'parameters' , source . postData . params , opts ) )
72+ code . push ( null )
73+ code . push ( util . format ( 'let boundary = "%s"' , source . postData . boundary ) )
74+ code . push ( null )
75+ code . push ( 'var body = ""' )
76+ code . push ( 'var error: NSError? = nil' )
77+ code . push ( 'for param in parameters {' )
78+ code . push ( indent + 'let paramName = param["name"]!' )
79+ code . push ( indent + 'body += "--\\(boundary)\\r\\n"' )
80+ code . push ( indent + 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""' )
81+ code . push ( indent + 'if let filename = param["fileName"] {' )
82+ code . push ( indent + indent + 'let contentType = param["content-type"]!' )
83+ code . push ( indent + indent + 'let fileContent = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error)' )
84+ code . push ( indent + indent + 'if (error != nil) {' )
85+ code . push ( indent + indent + indent + 'println(error)' )
86+ code . push ( indent + indent + '}' )
87+ code . push ( indent + indent + 'body += "; filename=\\"\\(filename)\\"\\r\\n"' )
88+ code . push ( indent + indent + 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"' )
89+ code . push ( indent + indent + 'body += fileContent!' )
90+ code . push ( indent + '} else if let paramValue = param["value"] {' )
91+ code . push ( indent + indent + 'body += "\\r\\n\\r\\n\\(paramValue)"' )
92+ code . push ( indent + '}' )
93+ code . push ( '}' )
6594 break
6695
6796 default :
0 commit comments