0

I'm using AFNetworking 2.0. I implemented my singleton class as subclass of: AFHTTPSessionManager

+ (RESTClientManager *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://myurl.com:8080"]];
    });

    return _sharedInstance;
}


- (instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];

    if (self) {
        self.responseSerializer = [AFJSONResponseSerializer serializer];
        self.requestSerializer = [AFJSONRequestSerializer serializer];
    }

    return self;
}

When I need to make a POST I use this code:

[self POST:@"/users" parameters:requestParameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Ok");

    }
    failure:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Failed");
    }];

But I obtain the error:

{ status code: 406, headers {
    Connection = "keep-alive";
    "Content-Length" = 0;
    "Content-Type" = "application/json";
    Date = "Sat, 30 Aug 2014 09:29:15 GMT";
    Server = "8";
    "X-Powered-By" = "Undertow 1";
} }, NSLocalizedDescription=Request failed: unacceptable (406)}

Using a RESTful client I tried to make the same request: This work well when I add this header fields:

  • Accept": application/json
  • Content-Type : application/json

How can I have this using AFNetworking 2.0??


EDIT

Print of Parameters Dictionary

po requestParameters
{
    city = "";
    country = Italy;
    deviceToken = "";
    email = "email@sdsd.it";
    mobileNumber = "";
    name = "Test_iOS";
    phoneNumber = "";
    postalCode = "";
    street = "";
    streetNumber = "";
    surname = "";
    userId = 00000001;
}
Safari
  • 11,437
  • 24
  • 91
  • 191

1 Answers1

0

If you've added the proper Content-Type to your request header, then your parameters are probably the source of the error. If you can contact anyone in charge of the backend server, you may be able to get an exact error report from them. If not, make sure the parameters aren't null and what you are passing follow whatever standards the server uses.

We could help you better if we could see the parameters passed.

  • I generate an NSDictionary and I pass it in this line: [self POST:@"/users" parameters:requestParameters success:... – Safari Aug 30 '14 at 13:25
  • I edited my question: Now you can found a print of Dictionary parameters. Tnx – Safari Aug 30 '14 at 13:39
  • Are you sure your service returns a JSON response? http://stackoverflow.com/questions/14251851/what-is-406-not-acceptable-response-in-http – Abhijay Bhatnagar Aug 30 '14 at 14:39
  • In the specification document of the work I read that these parameters should be a Json: just read: Body JSON Contains the user details. I did not understand: Have I to add this Json in the body of the request as NSData or can I pass the NSDictionary to the POST method of AFNetworking??? – Safari Aug 30 '14 at 15:03
  • Error code 406 is when "Your backend service is saying that the response type it is returning is not provided in the Accept-Type HTTP header in your Client requests" Meaning that for some reason your content-type and accept haven't been set to Json, even though you've already done so. Try to nslog self.requestserializers.allhttpheaders right before you do your POST request. (I'm writing that from memory so the actually property name may be simply different) – Abhijay Bhatnagar Aug 30 '14 at 15:24