This code I use in Upload (I found it in a tutorial) is working successfully but the parameters is not being recognised instead of WHERE emp_name ='(insert employee)'it says emp_name='' because i'm trying to update the URL with the newly uploaded picture. Please help. i'm new to this.
func UploadRequest()
{
let url = URL(string: "http://www.example.com/empupload.php")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (imageDetail.image == nil)
{
return
}
let image_data = UIImagePNGRepresentation(imageDetail.image!)
if(image_data == nil)
{
return
}
let body = NSMutableData()
let loadempname = NSMutableURLRequest()
let fname = "test.png"
let mimetype = "image/png"
let postString = "empName=\(empname.text!))"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)
body.append(postString.data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(
data, response, error) in
guard ((data) != nil), let _:URLResponse = response, error == nil else {
print("error")
return
}
if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
{
print(dataString)
}
})
task.resume()
}
You can post image through Alamofire multipartFormData in swift3.0. Here is sample code:
let parameters = ["name":"YourName"]
Alamofire.upload(.POST, baseUrl, multipartFormData:
{
multipartFormData in
if let imageData = UIImageJPEGRepresentation(self.imageView.image!, 0.6)
{
multipartFormData.appendBodyPart(data: imageData, name: "keyName", fileName: "photo.jpg", mimeType: "image/jpeg")
}
for (key, value) in parameters
{
if value is String || value is Int
{
multipartFormData.appendBodyPart(data: "\(value)".dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
}
}, encodingCompletion:
{
encodingResult in
switch encodingResult
{
case .Success(let upload, _, _):
upload.responseJSON { response in
print(response)
if String(response.result) == "SUCCESS"
{
if(response.result.value!.objectForKey("status")) as! String != "fail"
{
// here you can write success case
}
else
{
}
}
else
{
MyLoadingView.dismiss();
MyAlertView.showErrorAlert("Please try Again" )
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
Related
I'm trying to make a call to the OCR.space API by uploading a base 64 image but I keep getting this response:
Not a valid base64 image. The accepted base64 image format is 'data:image/;base64,'
I've checked the image many times to make sure it's encoded properly in base 64 format. Am I setting up the URLRequest properly?
Here is the code:
func extractTextFromImage(_ image: CGImage) {
let bitmapRep = NSBitmapImageRep(cgImage: image)
let imageData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])! as Data
let imageString = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
let url = URL(string: "https://api.ocr.space/parse/image")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("6ea787d56088957", forHTTPHeaderField: "apikey")
var parameters = ["base64image": [ "content_type": "image/jpeg", "base64": imageString]]
do {
let jsonData = try JSONSerialization.data(withJSONObject: parameters, options: [])
let theJSONText = String(data: jsonData, encoding: String.Encoding.utf8)
print("JSON string = \(theJSONText)")
request.httpBody = jsonData
} catch {
print(error.localizedDescription)
print("ERROR: Could not convert dictionary to JSON")
return
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("6ea787d56088957", forHTTPHeaderField: "apikey")
let task: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
if let response = response {
print("RESPONSE: ", response)
} else {
print("ERROR: No response")
}
if let error = error {
print("ERROR: ", error)
} else {
print("No error")
}
do {
let dictionary = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
for (key, value) in dictionary {
print("Key: ", key)
print("Value: ", value)
}
if let parsedResults = dictionary["ParsedResults"] as? [[String: Any]] {
if let parsedResult = parsedResults.first {
if let parsedText = parsedResult["ParsedText"] as? String {
print("PARSED TEXT ", parsedText)
} else {
print("ERROR: Could not read parsedText")
}
} else {
print("ERROR: Could not read first element of parsedResult")
}
} else {
print("ERROR: Could not read parsedResult")
}
} catch let error {
print("ERROR: Could not serialize jSON Data: \(error.localizedDescription)")
}
})
task.resume()
}
After alot of reading and searching, this is the code that I got to work:
///
/// Extracts the text from the given image by placing a call to the OCR.space web api
///
func extractText(from image: CGImage) {
let bitmapRep = NSBitmapImageRep(cgImage: image)
let imageData = bitmapRep.representation(using: NSBitmapImageRep.FileType.png, properties: [:])! as Data
let url = URL(string: "https://api.ocr.space/parse/image")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "--------69-69-69-69-69"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("6ea787d56088957", forHTTPHeaderField: "apikey")
request.httpBody = createBody(parameters: nil, filePathKey: "file", imageDataKey: imageData, boundary: boundary)
let task = session.synchronousDataTask(urlrequest: request)
let data = task.0
let error = task.2
if data == nil {
print("ERROR: No response from OCR.space api call")
return
}
if error != nil {
print("ERROR: ", error!)
return
}
do {
let dictionary = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
if let parsedResults = dictionary["ParsedResults"] as? [[String: Any]] {
if let parsedResult = parsedResults.first {
if let text = parsedResult["ParsedText"] as? String {
parsedText = text
return
} else {
print("ERROR: Could not read parsedText")
return
}
} else {
print("ERROR: Could not read first element of parsedResult")
return
}
} else {
print("ERROR: Could not read parsedResult")
return
}
} catch let error {
print("ERROR: Could not serialize jSON Data into dictionary: \(error.localizedDescription)")
return
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
///
/// Creates a the body of the url request using the given parameters
///
private func createBody(parameters: [String: String]?, filePathKey: String?, imageDataKey: Data, boundary: String) -> Data {
var body = Data();
if parameters != nil {
for (key, value) in parameters! {
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(value)\r\n".data(using: String.Encoding.utf8)!)
}
}
let filename = "image.jpg"
let mimetype = "image/jpg"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(imageDataKey)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
return body
}
i want to upload photo into server with almofire i'm using this code right now but i'm getting timeout
let imageData = UIImageJPEGRepresentation(croppedImage, 1)
let urlRequest = self.urlRequestWithComponents(url, imageData: imageData!)
self.alamofire.upload(urlRequest.1, to: urlRequest.0 as! URLConvertible).responseJSON { response in
guard response.result.isSuccess else {
self.showError()
return
}
guard let data = response.result.value else {
self.showError()
return
}
let json = JSON(data)
}
func urlRequestWithComponents(_ urlString:String, imageData:Data) -> (URLRequestConvertible, Data) {
var urlRequest = URLRequest(url: URL(string: urlString)!)
urlRequest.httpMethod = HTTPMethod.post.rawValue
let boundary = generateBoundaryString()
let contentType = "multipart/form-data;boundary=" + boundary
urlRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
let uploadData = NSMutableData()
uploadData.append("\r\n--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
uploadData.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".data(using: String.Encoding.utf8)!)
uploadData.append("Content-Type: image/png\r\n\r\n".data(using: String.Encoding.utf8)!)
uploadData.append(imageData)
uploadData.append("\r\n--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
return try! (Alamofire.URLEncoding.default.encode(urlRequest, with: nil), uploadData as Data)
}
Is there any solution about this issue ?
Try this in swift 3
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
}, to:"http://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
I am trying to upload data to our server. The data is a zip file converted in to Data.
SSZipArchive.createZipFile(atPath: "\(self.currentWPDirectory!)/\(self.documentId!).zip", withFilesAtPaths: checkInFileList)
let zipData = try! Data(contentsOf: URL(string: "file:///\(self.currentWPDirectory!)/\(self.documentId!).zip")!)
URLSession
let parameters = "?documentId=\(self.documentId!)&documentAction=CHECKIN&doc_action_comment=some comment&device_name=\(UIDevice.current.name)"
let urlString = "\(Constants.WebService.checkIn)\(parameters)"
let url = urlString.replacingOccurrences(of: " ", with: "%20")
let request = NSMutableURLRequest(url: URL(string: url)!)
let data = zipData
request.httpMethod = HTTPMethod.post.rawValue
let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let contentType = "multipart/form-data; boundary=\(boundary)"
request.setValue(contentType, forHTTPHeaderField:"Content-Type")
let body = NSMutableData();
let tempData = NSMutableData()
let fileName = "FILE_NAME" + ".zip"
let parameterName = "userfile"
let mimeType = "application/octet-stream"
tempData.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
let fileNameContentDisposition = fileName != "" ? "filename=\"\(fileName)\"" : ""
let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\"; \(fileNameContentDisposition)\r\n"
tempData.append(contentDisposition.data(using: String.Encoding.utf8)!)
tempData.append("Content-Type: \(mimeType)\r\n\r\n".data(using: String.Encoding.utf8)!)
tempData.append(zipData)
tempData.append("\r\n".data(using: String.Encoding.utf8)!)
body.append(tempData as Data)
body.append("\r\n--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.httpBody = body as Data
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.uploadTask(with: request as URLRequest, from: data) { (data, response, err) in
// Gets 206 in response
}
task.resume()
Alamofire
let params = [
"documentId": self.documentId!,
"documentAction": (self.isSync) ? "SYNC" : "CHECKIN",
"doc_action_comment": "Some Comment",
"device_name": UIDevice.current.name
]
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(zipData, withName: "FILE_DATA", fileName: "\(self.documentId!).zip", mimeType: "application/zip")
for (key, value) in params {
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
},
to: "\(Constants.WebService.checkIn)?documentId=\(self.documentId!)&documentAction=\((self.isSync) ? "SYNC" : "CHECKIN")&doc_action_comment=comment&device_name=\(UIDevice.current.name)",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response)
// Get 406 Response code
// Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0."
}
self.completionCallback!(true)
}
case .failure(let encodingError):
print(encodingError)
self.completionCallback!(false)
}
}
)
I have tried many ways to get multipart form/data uploads to send my data, with no success. Can anyone point me in the right direction?
I'm trying to upload an image to the server. As for now, I get an error response which checks if name='image', and the error means that it's not. The line where I set it is this:
body.appendString("Content-Disposition: form-data; name='image'; filename='test.jpg'")
my full code of the POST request is this: I do get a 200 and the only problem is with the name parameter which I really can't figure out.
func imageUploadRequest()
{
let stringUrl = "http://88.162.41.55/app_backend/public/api/v1/image?_r=1836486547600309"
let URL = NSURL(string: stringUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!
let request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "POST";
request.setValue("Bearer \(jwtToken)", forHTTPHeaderField: "Authorization")
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(image, 1)
if(imageData == nil) {
print("image data is nil")
return
}
let body:NSMutableData = NSMutableData()
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name='image'; filename='test.jpg'")
body.appendString("Content-Type: image/jpg")
body.appendData(imageData!)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
request.HTTPBody = body
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(" response = \(responseString!)")
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
print("json", json)
} catch {
print("bad things happened")
}
}
task.resume()
}
Any ideas? Thank you so much!!
Sample NSURLSession
func getMetaData(lePath:String, completion: (string: String?, error: ErrorType?) -> Void) {
// **** get_metadata ****
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.dropboxapi.com/2/files/get_metadata")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("Bearer ab-blah", forHTTPHeaderField: "Authorization")
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("path", forHTTPHeaderField: lePath)
let cursor:NSDictionary? = ["path":lePath]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(cursor!, options: [])
request.HTTPBody = jsonData
print("json ",jsonData)
} catch {
print("snafoo alert")
}
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let error = error {
completion(string: nil, error: error)
return
}
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
//print("Body: \(strData)\n\n")
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers);
completion(string: "", error: nil)
} catch {
completion(string: nil, error: error)
}
})
task.resume()
}
I am learning swift and I send a request to the server with the code below. It works for simple request and I get response from the server. My problem is I can not send a file to server.
code :
let parameters = parameter
let request = NSMutableURLRequest(URL: NSURL(string: requestUrl)!)
let boundaryConstant = "-----Boundary+\(arc4random())\(arc4random())"
let contentType = "multipart/form-data; boundary=" + boundaryConstant
let boundaryStart = "--\(boundaryConstant)\r\n"
let boundaryEnd = "--\(boundaryConstant)--\r\n"
let body:NSMutableString = NSMutableString();
for (key, value) in parameters {
body.appendFormat(boundaryStart)
body.appendFormat("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendFormat("\(value)\r\n")
}
body.appendFormat(boundaryEnd)
request.HTTPMethod = "POST"
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
// check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
self.responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print("MMMMMMMM \(self.responseString)")
self.result = self.responseString.dataUsingEncoding(NSUTF8StringEncoding)! as NSData
callback(self.responseString)
}
print("code start")
task.resume()
result :
i can post file to server by this code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let request = createRequest()
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
if error != nil {
// handle error here
print(error)
return
}
do {
if let responseDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print("success == \(responseDictionary)")
}
} catch {
print(error)
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
}
task.resume()
}
func createRequest () -> NSURLRequest {
let param = []
let boundary = generateBoundaryString()
let url = NSURL(string: "URl")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("userValue", forHTTPHeaderField: "X-Client-user")
request.setValue("passValue", forHTTPHeaderField: "X-Access-pass")
//let path1 = NSBundle.mainBundle().pathForResource("voice", ofType: "png") as String!
request.HTTPBody = createBodyWithParameters(param, filePathKey: "voice", paths: ["pathURl"], boundary: boundary)
return request
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, paths: [String]?, boundary: String) -> NSData {
let 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")
}
}
if paths != nil {
for path in paths! {
let url = NSURL(fileURLWithPath: path)
let filename = url.lastPathComponent
let data = NSData(contentsOfURL: url)!
let mimetype = mimeTypeForPath(path)
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(data)
body.appendString("\r\n")
}
}
body.appendString("--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream";
}
As you read here, you should use NSURLSession for HTTP work, it far more flexible and powerful; and I think is destined to replace NSURLconnection...
https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/
Here is a example for you...
func getMetaData(lePath:String, completion: (string: String?, error: ErrorType?) -> Void) {
// **** get_metadata ****
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.dropboxapi.com/2/files/get_metadata")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("Bearer ab-blah-blah", forHTTPHeaderField: "Authorization")
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("path", forHTTPHeaderField: lePath)
let cursor:NSDictionary? = ["path":lePath]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(cursor!, options: [])
request.HTTPBody = jsonData
print("json ",jsonData)
} catch {
print("snafoo alert")
}
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let error = error {
completion(string: nil, error: error)
return
}
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)\n\n")
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers);
self.jsonParser(jsonResult,field2file: "ignore")
for (key, value) in self.parsedJson {
print("key2 \(key) value2 \(value)")
}
completion(string: "", error: nil)
} catch {
completion(string: nil, error: error)
}
})
task.resume()
}
Great answer above.. Here it's updated for Swift3:
func getMetaData(lePath:String, completion: (string: String?, error: ErrorType?) -> Void) {
// **** get_metadata ****
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.dropboxapi.com/2/files/get_metadata")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("Bearer ab-blah-blah", forHTTPHeaderField: "Authorization")
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("path", forHTTPHeaderField: lePath)
let cursor:NSDictionary? = ["path":lePath]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(cursor!, options: [])
request.HTTPBody = jsonData
print("json ",jsonData)
} catch {
print("snafoo alert")
}
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let error = error {
completion(string: nil, error: error)
return
}
let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Body: \(strData)\n\n")
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers);
self.jsonParser(jsonResult,field2file: "ignore")
for (key, value) in self.parsedJson {
print("key2 \(key) value2 \(value)")
}
completion(string: "", error: nil)
} catch {
completion(string: nil, error: error)
}
})
task.resume()
}