Search code examples
swiftafnetworking-2

Request Failed: unacceptable content-type: text/html - AFNetworking 2 in Swift


All,

I am trying to GET JSON from a url using AFNetworking in SWIFT. I am getting the error : Request failed unacceptable content-type : text/html

here is my code :

func makeGet() {
    let manager = AFHTTPRequestOperationManager()
    manager.requestSerializer.
    manager.GET(apiURL,
        parameters: nil,
        success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
            self.jsonLoaded(responseObject.description)
        },
        failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
            self.jsonFailed(error)
        }
    )

I intend to use :

manager.requestSerializer.setValue(<#value: String!#>, forHTTPHeaderField: <#String!#>)

to set the content-type to text/html.

any ideas how to do this ?

I have just added :

 manager.requestSerializer.setValue("text/html", forHTTPHeaderField: "Content-Type")

this has not helped.


Solution

  • Try this:

    manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json"]) as Set<NSObject>

    or even faster:

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];