Search code examples
iosjsonswiftalamofireswifty-json

Alamofire Completion Handler returning response + data


I have a function that includes a responseObject in it's completion handler. At the moment this returns the json body when I call it, along with any errors.

Some parts of the API I am using however, don't return any data in the response body, they just return a response (200, 404, etc...)

I was thinking about appending the response inside the empty json object that is getting returned, then realised that would be silly and it would probably be better if I returned the NSHTTPURLResponse as well, but everything I have found just explains how to return the responseObject along with the error...

This is the function that returns the completion handler:

func makePostRequest(url : String, params : AnyObject, completionHandler: (responseObject:  NSHTTPURLResponse, JSON?, error: NSError?) -> ())  -> Request? {


    println("params = \(params)")
    return Alamofire.request(.POST, url, parameters: params as? [String : AnyObject], encoding: .JSON)


        .responseJSON { (request, response, data, error) in completionHandler(
            //This is wrong
            response: response as? NSHTTPURLResponse,
            responseObject:
            {
                println("Request is \(request)")

                println("Response is \(response)")

                println("Data is \(data)")

                println("Error is \(error)")

                //Append the response to this JSON object?
                //
                var json:JSON = [:]


                if let anError = error
                {

                    println(error)
                }
                else if let data: AnyObject = data
                {

                    json = JSON(data)

                }


                //probably better to return the two...
                //
                return (response, json)

                }(),
            error: error

            )
    }
}

And this is how its used:

networking.makePostRequest(documentUrl, params: docParams) { response, json, error  in


           println("response is: \(response)")

        println("document json: \(json)")

         println("document error: \(error)")



    }

I've added in the 'response' bits to all the bits of code, i'm sure this is possible? just not sure how to achieve it..


Solution

  • For anyone stuck trying to figure out how to return stuff this way, I solved it like this:

     func makePostRequest(url : String, params : AnyObject, completionHandler: (httpResponse: NSHTTPURLResponse, responseObject:JSON?, error: NSError?) -> ())  -> Request? {
    
    
        println("params = \(params)")
        return Alamofire.request(.POST, url, parameters: params as? [String : AnyObject], encoding: .JSON)
    
    
            .responseJSON { (request, response, data, error) in completionHandler(
                //This is wrong
                httpResponse: response!,
                responseObject:
                {
                    println("Request is \(request)")
    
                    println("Response is \(response)")
    
                    println("Data is \(data)")
    
                    println("Error is \(error)")
    
                    //Append the response to this JSON object?
                    //
                    var json:JSON = [:]
    
    
                    if let anError = error
                    {
    
                        println(error)
                    }
                    else if let data: AnyObject = data
                    {
    
                        json = JSON(data)
    
                    }
                     return json
                }(),
                error: error
    
                )
        }
    }
    

    and then calling it like this:

     networking.makePostRequest(workDocumentUrl, params: params) { response, json, error  in
    
            if response.statusCode == 200{
           //do something
    
            }
    
            println("json: \(json)")
            println("error: \(error)")
    
    
    
        }