I am using AFNetworking 2.0 in swift for calling The api is as follows http://api.openweathermap.org/data/2.5/weather?q=Delhi
When we hit this URL on browser, we will get a JSON. This is a GET request. On hitting this api on postman also produces the same result Now I made a helper class WebService.swift and in which I am calling a success and a error block
My WebService.swift class is as follows
import UIKit
class WebService: NSObject {
static let sharedInstance = WebService()
func weatherService(loginCode:String, requestData:NSMutableDictionary, successBlock: (response: AnyObject!,headers: NSDictionary, callBack:String ) -> Void, errorBlock: (error: NSError,headers:NSDictionary,responseObject:AnyObject!) -> Void){
var serviceURL = "http://api.openweathermap.org/data/2.5/weather?q=Delhi"
var manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/html","application/json") as? Set<NSObject>
manager.requestSerializer.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
manager.GET(serviceURL, parameters: requestData, success: { (operation:AFHTTPRequestOperation, responseObject:AnyObject) -> Void in
var headersCollection = operation.valueForKey("response")?.valueForKey("allHeaderFields") as! NSDictionary
var headers = headersCollection.mutableCopy() as! NSMutableDictionary
headers["statusCode"] = NSNumber(integer: operation.response!.statusCode)
var _callBack = requestData["callBack"] as! String
successBlock(response:responseObject,headers:headers,callBack: _callBack)
}) { (operation:AFHTTPRequestOperation, error:NSError) -> Void in
var headersCollection = operation.valueForKey("response")?.valueForKey("allHeaderFields") as! NSDictionary
var headers = headersCollection.mutableCopy() as! NSMutableDictionary
headers["statusCode"] = NSNumber(integer: operation.response!.statusCode)
errorBlock(error: error, headers: headers, responseObject: operation.responseObject)
}
}
}
The method I am calling from my view controller in view did load
let params = ["callBack": String(format: "%d", "weatherAPI")] as NSMutableDictionary
WebService.sharedInstance.weatherService("URLHardcodedInWebServiceClass", requestData: params, successBlock: { (response, headers, callBack) -> Void in
var dict = response as! NSDictionary
println("Success with \(callBack)")
}) { (error, headers, responseObject) -> Void in
println("failed")
}
Now the problem is that everytime I am going in the error block of webservice class where the contents of headers which is a NSMutableDictionary is
{
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Methods" = "GET, POST";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 31 Aug 2015 18:53:36 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
"X-Source" = back;
statusCode = 200;
}
And when I see the NSError it shows
Printing description of error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"
(JSON text did not start with array or object and option to allow fragments not set.)
UserInfo=0x7fdb6865d5a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Status code I receive is 200
And when I do this
po operation.responseString I get

The json is there but with errors and I guess it is not formed correctly. I tried my other service with content-type as text/html and it worked fine as I have to change the line
manager.requestSerializer.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
And URL for other service is http://bluesapphireindia.in/WebService.asmx/GetEmployessJSON
It is working fine in this piece of code
What can be the reason. Can anyone help me out in this. Thanks