         request.setValue("application/json", forHTTPHeaderField: "Content-Type")
         request.httpBody = jsonData
         request = authenticate(request)
+        print("PUT to \(url) -> \(request)")
         
         let session = URLSession(configuration: URLSessionConfiguration.ephemeral)
-        task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
+        task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
             
             guard let httpStatus = response as? HTTPURLResponse, let data = data, error == nil else {
-                print("PUT \(url) -> \(error!)")
+                print("PUT to \(url) -> \(error!)")
                 failure(error!)
                 return
             }
-            print("status code = \(httpStatus.statusCode)")
-            //            RCLogO( NSString(data: data!, encoding: 0));
-            
-            if let d = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)  {
-                success(d)
-            } else {
-                print("Not a valid json for url \(url)")
-                failure(RCHttpError(errorDescription: "Invalid json response"))
-            }
+            print("PUT to \(url) response: \(String(data: data, encoding: .utf8) ?? "") -> status code = \(httpStatus.statusCode)")
+            success(data)
         })
-        task?.resume()
+        task!.resume()
     }
     
 	func upload (data: Data, filename: String, completion: @escaping (NSDictionary) -> Void, error: @escaping (NSDictionary) -> Void) {

	
    var baseURL: URL! = nil
	private var task: URLSessionTask?
    private var user: String?
    private var password: String?
	
	convenience init (baseURL: String) {
		self.init()
		self.baseURL = URL(string: baseURL)
	}
    
    func authenticate (user: String, password: String) {
        self.user = user
        self.password = password
    }
	
    
    func get (at path: String, success: @escaping (Any) -> Void, failure: @escaping (Error) -> Void) {
        
        guard baseURL != nil else {
            fatalError("URL was not set")
        }
        let fullPath = baseURL.appendingPathComponent(path).absoluteString.removingPercentEncoding!
        let url = URL(string: fullPath)!
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        
        // Authenticate
        request = authenticate(request)
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let httpStatus = response as? HTTPURLResponse, let data = data, error == nil else {
                print("GET \(url) -> \(error!)")
                failure(error!)
                return
            }
            print("status code = \(httpStatus.statusCode)")
//            print(String(data: data, encoding: String.Encoding.utf8))
            if let d = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) {
                success(d)
            } else {
                print("Not a valid json for url \(url)")
                failure(RCHttpError(errorDescription: "Invalid json response"))
            }
//            print(data)
//            if let httpStatus = response as? HTTPURLResponse {
//                // check status code returned by the http server
//                print("status code = \(httpStatus.statusCode)")
//                // process result
//
//            }
        }
        task.resume()
    }
	
	//pragma mark post data sync and async

    func post (at path: String, parameters: [String: Any], success: @escaping (Any) -> Void, failure: @escaping (Error) -> Void) {

        guard baseURL != nil else {
            fatalError("URL was not set")
        }
        let fullPath = baseURL.appendingPathComponent(path).absoluteString.removingPercentEncoding!
        let url = URL(string: fullPath)!
        let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)

        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
		request.httpBody = jsonData
        request = authenticate(request)
        print("POST to \(url) -> \(request)")
		
		let session = URLSession(configuration: URLSessionConfiguration.ephemeral)
		task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
			
            guard let httpStatus = response as? HTTPURLResponse, let data = data, error == nil else {
                print("POST to \(url) -> \(error!)")
                failure(error!)
                return
            }
            print("POST to \(url) response: \(String(data: data, encoding: .utf8) ?? "") -> status code = \(httpStatus.statusCode)")
            success(data)
		})
		task!.resume()
	}
	
    //mark: put data sync and async
    
    func put (at path: String, parameters: [String: Any], success: @escaping (Data) -> Void, failure: @escaping (Error) -> Void) {
        
        let fullPath = baseURL.appendingPathComponent(path).absoluteString.removingPercentEncoding!
        let url = URL(string: fullPath)!
        let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
