dimanche 14 mai 2017

Alamofire Upload get returned JSON

question on getting at the result data in an Alamofire.upload call.

I am sending multipart form data to a server using the following code:

    sessionManager.upload(
        multipartFormData: { multipartFormData in
            if let name = currentPlace.name,
                let data = name.data(using:String.Encoding.utf8) {
                multipartFormData.append(data, withName: "placename", mimeType: "text/plain")
            }
            if let lat = currentPlace.location?.coordinate.latitude {
                multipartFormData.append(String(describing: lat).data(using:String.Encoding.utf8)!, withName: "latitude", mimeType: "text/plain")
            }
            if let lon = currentPlace.location?.coordinate.longitude {
                multipartFormData.append(String(describing: lon).data(using:String.Encoding.utf8)!, withName: "longitude", mimeType: "text/plain")
            }

            multipartFormData.append((User.current.scanMode.rawValue).data(using:String.Encoding.utf8)!, withName: "state", mimeType: "text/plain")
    },
        to: (url?.absoluteString)!,  headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.responseJSON { response in

                    upload.responseJSON { response in
                        print(response.request)  // original URL request
                        print(response.response) // URL response
                        print(response.data)     // server data
                        print(response.result)   // result of response serialization

                        if let JSON = response.result.value as? NSArray {
                            for element in JSON {
                                print(element)
                            }
                        }
                    }

                }
            case .failure(let encodingError):
                print(encodingError)
            }
    }
    )

My challenge is to get the value of response.result.value in a form that I can validate. The current debug output is:

(lldb) po response.result.value
▿ Optional<Any>
  ▿ some : 4 elements
    ▿ 0 : 2 elements
      - key : code
      - value : 200
    ▿ 1 : 2 elements
      - key : id
      - value : 78230c53954a3adbf14b49cda127bf55
    ▿ 2 : 2 elements
      - key : message
      - value : Successfully updated OID to DISTRIBUTED.
    ▿ 3 : 2 elements
      - key : state
      - value : DISTRIBUTED

Looks like a array, but can't seem to get this casted or reflected into something I can use. Usually I would use EVOReflection to get at this data as in:

 sessionManager.request((url?.absoluteString)!, method: .get, parameters: postParameters, encoding: URLEncoding.httpBody, headers: headers)
            .responseObject { (response: DataResponse<AssetStateResponse>) in
                if let response = response.result.value {
                    completionHandler(response)
                } else {
                    // LATER: Better error handling
                }
        }

Any help appreciated, have been looking at this for a few days now, missing something simple I suspect.





Aucun commentaire:

Enregistrer un commentaire