Dunbah
Dunbah

Reputation: 99

AFNetorking in Swift project -- "Error: Request failed: unacceptable content-type: text/html"

I am implementing AFNetworking with CocoaPods into a Swift project. I am used to programming in Ruby and I am very new to iOS development. Cocoapods was tricky to get working properly in my project but I can now successfully access the AFNetworking library.

What I am trying to do is hit a form with a POST and get a response as "text/html" that I can parse so that I can tell if it saves. This is not an API per se, but a form generated by InfusionSoft. A user will enter an email address and I will send that off to an API for storage. Here is the code I am using:

let manager = AFHTTPRequestOperationManager()
            var parameters = ["inf_form_xid": "MY_ACCESS_ID",
                      "inf_form_name": "Webform in Content App",
                      "infusionsoft_version": "1.34.0.35",
                      "inf_field_email": self.emailTextField.text]
    
    manager.POST( "https://ns166.infusionsoft.com/app/form/process/REALLYLONGUNIQUEID",
    parameters: parameters,
    success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
        println("JSON: " + responseObject.description)
    },
    failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
        println("Error: " + error.localizedDescription)
    })

And the error that I am receiving this as a response from AFNetworking:

Error: Request failed: unacceptable content-type: text/html

At the end of the day, I want to verify that it was saved by the server before I allow the user to continue in the application.

Any help is greatly appreciated.

Upvotes: 2

Views: 4639

Answers (3)

ABS
ABS

Reputation: 7732

I am not sure about swift but in objective c ,after searching for answer i came across the solution which working very well for me . Below is the code snippet:

AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];

NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;

Upvotes: 0

Pintouch
Pintouch

Reputation: 2619

You have to add content-type allowed for the response.

You could do that for json content :

 manager.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json")

EDIT

If you're using AFNetworking 2.0, from AFNetworking wiki :

"By default, AFHTTPRequestOperationManager and AFHTTPSessionManager have JSON serializers."

So you should maybe try to change the response serializer in your manager like this :

manager.responseSerializer = AFHTTPResponseSerializer()

Upvotes: 5

rahulinaction
rahulinaction

Reputation: 354

This means that your server is sending "text/html" instead of the already supported types.You can either add acceptable content type as "text/html" or ensure that you are properly sending json format from the server side.By default the content type for afnetworking is json in objective c.Dont know about swift. Reference :Request failed: unacceptable content-type: text/html using AFNetworking 2.0

Upvotes: 0

Related Questions