Multipart / Swift Submit Request

My code correctly performs an HTTP POST request to my PHP endpoint. However, all the data I need to send is not coming. This is the code that I have for sending the image and other parameters. The problem I ran into is that the PHP code does not recognize key value pairs (i.e. $ _POST ['DATE']). In my PHP code, I return all the values ​​of the POST dictionary, and they come out empty.

var imageData : NSData = UIImageJPEGRepresentation(Record.sharedInstance.picture, 1.0)

    let url = NSURL(string:"URL")
    let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)

    request.HTTPMethod = "POST"

    let boundary    = "Boundary-\(NSUUID().UUIDString)"
    let contentType = "mulitpart/form-data; boundary=\(boundary)"

    request.setValue(contentType, forHTTPHeaderField: "Content-Type")

    var body = NSMutableData()

    var tempData      = NSMutableData()
    let fileName      = "sharkImage" + ".jpg"
    let parameterName = "PHOTOGRAPH"

    let mimeType = "image/jpeg"

    let fileNameContentDisposition = String(fileName) != nil ? "filename=\"\(fileName)\"" : ""
    let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\";\(fileNameContentDisposition)\r\n"

    println(Record.sharedInstance.mImagePath)


    tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
    tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
    tempData.appendData("Content-Type : \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
    tempData.appendData(imageData)
    tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)

    body.appendData(tempData)
    body.appendData(buildBody("DATE", boundary: boundary, value: Record.sharedInstance.mDate))
    body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
    request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = body

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, response, error in

    if error != nil {
        return
    }

    // if response was JSON, then parse it
    var parseError: NSError?
    let responseObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError)
        if let responseDictionary = responseObject as? NSDictionary {                
            let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)

        println("responseString = \(responseString)")

    } else { // if not JSON
            let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("responseString = \(responseString)")
    }

    })

    task.resume()
+4
source share

Source: https://habr.com/ru/post/1570255/


All Articles