Maxwell Bo Blessen
Maxwell Bo Blessen

Reputation: 173

Alamofire .GET returning nil for response and JSON

this code is returning nil for the response and the json, any idea what the problem is? the url is a link to an api endpoint

import UIKit
import Alamofire


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func loadData(){

        let url = "http://httpbin.org/get"

        Alamofire.request(.GET, url).responseJSON { (request, response, json, error) in
            println(request)
            println(response)
            println(json)



    }



    }
}

it returns this

{ URL: http://httpbin.org/get } nil nil

Upvotes: 0

Views: 4469

Answers (1)

cnoon
cnoon

Reputation: 16643

Always make sure to check your error case. If an error occurred, Alamofire will make sure to bubble it up to your response serializer. Your request is failing and since you are not printing out the error or checking against it, you don't know that that is the problem. If you update your loadData method to the following, you will have a much better idea of how to proceed.

func loadData() {
    let request = Alamofire.request(.GET, "http://httpbin.org/get")
        .responseJSON { (request, response, json, error) in
            println(request)
            println(response)
            println(json)
            println(error)
    }

    debugPrintln(request)
}

If the error message doesn't provide you enough information, then you can move to the cURL command that is generated by the debugPrintln message.

Upvotes: 3

Related Questions