Swift - multipart/form-data - 如何定义boundary/fill HTML body?

Swift - multipart/form-data - How to define boundary/fill HTML body?

我正在尝试为 Swift 中的 multipart/form-data-request 定义边界。我的问题是如何在 Swift 中定义这个边界(最有效的方法)。我想发送一个带有 ID 和姓名等数据的请求。

实际状态:

 let url = NSURL(string: "http://test.bla")
 let request = NSMutableURLRequest(URL: url!)

 // Set HTTPMethod for actual request
 request.HTTPMethod = "POST"

实际问题:

这是上传图片需要定义边界的代码

    var TWITTERFON_FORM_BOUNDARY:String = "AaB03x"
    let url = NSURL(string: url)!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 10)
    var MPboundary:String = "--\(TWITTERFON_FORM_BOUNDARY)"
    var endMPboundary:String = "\(MPboundary)--"
    var data:NSData = UIImagePNGRepresentation(image)
    var body:NSMutableString = NSMutableString();
    // params
    if parameters != nil {
        for (key, value) in parameters! {
            body.appendFormat("\(MPboundary)\r\n")
            body.appendFormat("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendFormat("\(value)\r\n")
        }
    }
    // image upload
    body.appendFormat("%@\r\n",MPboundary)
    body.appendFormat("Content-Disposition: form-data; name=\"\(filename)\"; filename=\"pen111.png\"\r\n")
    body.appendFormat("Content-Type: image/png\r\n\r\n")
    var end:String = "\r\n\(endMPboundary)"
    var myRequestData:NSMutableData = NSMutableData();
    myRequestData.appendData(body.dataUsingEncoding(NSUTF8StringEncoding)!)
    myRequestData.appendData(data)
    myRequestData.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!)
    var content:String = "multipart/form-data; boundary=\(TWITTERFON_FORM_BOUNDARY)"
    request.setValue(content, forHTTPHeaderField: "Content-Type")
    request.setValue("\(myRequestData.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = myRequestData
    request.HTTPMethod = "POST"
    .........