AFNetworking 2.0 Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)

Nishith  Sheth picture Nishith Sheth · Mar 13, 2014 · Viewed 9.7k times · Source

I am trying to convert my code to AFNetworking 2.0 with sub classing AFHTTPRequestOperationManager . Here is my code

+ (NSAFNetwokingRequestManager *)sharedClient {
    static NSAFNetwokingRequestManager *_sharedClient = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:GET_CAR_BRAND]];
    });
    return _sharedClient;
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
     if (self) {
    self.responseSerializer = [AFXMLParserResponseSerializer serializer];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/soap+xml"];
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    [self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"];
}

    return self;
}
- (void)requestBrandcompletion:(NSAFNetwokingRequestManagerCompletionBlock)completion {
    NSString *soapRequest=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
    "<soap:Body>\n"
    " <CarBrandExt xmlns=\"http://www.nohausystems.nl/\" />\n"
    "</soap:Body>\n"
    "</soap:Envelope>\n";
    NSString *msgLength = [NSString stringWithFormat:@"%i",[soapRequest length]];
    [self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (completion) {
            completion(YES, responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (completion) {
            completion(NO, nil);
            NSLog(@"Unable to fetch record error %@ with user info %@.", error, error.userInfo);
        }
    }];
}

I am getting this error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500). Can any one tell me what am i doing wrong here ? Getting this response :

{ status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 509;
    "Content-Type" = "application/soap+xml; charset=utf-8";
    Date = "Thu, 13 Mar 2014 12:59:45 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
} }

Answer

lari picture lari · Mar 18, 2014

In your code you have:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];

which defineds content-type as "text/xml", while the server is clearly expecting "application/soap+xml". You should try changing that part of the code to:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/sopa+xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];

Updated suggestions:

Try adding:

[self.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

in the end of your - (instancetype)initWithBaseURL:(NSURL *)url method.

If this doesn't help, I would suggest doing some more detailed network request debugging. You could e.g. set up AFNetworkActivityLogger to log the request/response information to the console.