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;
}
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.