2

I want to upload image to server with other parameters using SWIFT and the server side script is in PHP. I'm posting the request to http://www.23look.com/merchant/verify which accepts following parameters:

mer_name - String

mer_tel - String

mer_address - string

lng - string

lat - string

mer_licence - file type

token - string

mer_type - string

Whenever I make HTTP post request, it always returns 0 value in response, which means "System Error". Maybe there's something wrong I'm doing, can someone please help me see my code?

func uploadImage()
{
    let postPictureUrl = NSURL(string: "http://www.23look.com/merchant/verify")
    let request = NSMutableURLRequest(URL: postPictureUrl!)
    request.HTTPMethod="POST"

    let param=[
        "mer_name" : shopNameUITF.text!,
        "mer_tel" : shopTelephoneUITF.text!,
        "mer_address" : shopAddressUITF.text!,
        "lat" : "39.6892",
        "lng" : "115.9239",
        "token": KeychainWrapper.stringForKey("tokenValue")!,
        "mer_type": "smalll"
    ]

    let abc =  KeychainWrapper.stringForKey("tokenValue")!

    let boundary = generateBoundaryString()

    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.1)

    if imageData==nil { print("image data is nil"); return }

    request.HTTPBody = createBodyWithParameters(param, filePathKey: "mer_license", imageDataKey: imageData!, boundary: boundary)

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

        if error != nil {
            print("error=\(error)")
            return
        }

        //You can print out response object
        print("***** response = \(response)")

        // Print out reponse body
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("**** response data = \(responseString!)")

        do {

            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

            dispatch_async(dispatch_get_main_queue(), {
                print(NSString(data: data!, encoding:NSUTF8StringEncoding)!)
                print(json)
            })

        } catch let err {
            print(err)
        }
    }
    task.resume()
}

func generateBoundaryString() -> String {
    return "Boundary-\(NSUUID().UUIDString)"
}

func createBodyWithParameters(parameters:[String:String]?, filePathKey: String?, imageDataKey:NSData, boundary: String) -> NSData {

    var body=NSMutableData()

    if parameters != nil {
        for(key, value) in parameters! {
            body.appendString("--\(boundary)\r\n")
            body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendString("\(value)\r\n")
        }
    }

    let filename = "user-profile.jpg"
    let mimetype = "image/jpg"

    body.appendString("--\(boundary)\r\n")
    body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
    body.appendString("Content Type: \(mimetype)\r\n\r\n")
    body.appendData(imageDataKey)
    body.appendString("\r\n")

    body.appendString("--\(boundary)--\r\n")

    return body
}  extension NSMutableData {
func appendString(string: String) {
    let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
    appendData(data!)
}
laser2302
  • 427
  • 2
  • 7
  • 19
  • Isn't it the response `0` given by the server? Seems not related to Swift – Raptor Jan 26 '16 at 07:14
  • Your final boundary is not correct, it should be `"--\(boundary)--"`. (no CRLF required). See [RFC 2046](https://tools.ietf.org/html/rfc2046#section-5.1) and [RFC 7578](https://tools.ietf.org/html/rfc7578). – CouchDeveloper Jan 26 '16 at 09:04
  • Even after changing the final boundary, it's not working. – laser2302 Jan 26 '16 at 10:15

1 Answers1

0

what if you do it with an appendData instead of appendString:

let body = NSMutableData()
let tempData = NSMutableData()
for (key, value) in parameters {
        tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        tempData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        tempData.appendData("\(value!)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        // etc etc
    }

body.appendData(tempData)

request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.HTTPBody = body
jasonnoahchoi
  • 871
  • 5
  • 16
  • I created an extension as following, so it should work: extension NSMutableData { func appendString(string: String) { let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) appendData(data!) } – laser2302 Jan 26 '16 at 10:08
  • Yes, it worked. The issue is fixed now, it was because I misspelled licence as license in mer_licence. – laser2302 Jan 27 '16 at 12:59