Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
} }
like image 256
Nishith Sheth Avatar asked Mar 13 '14 11:03

Nishith Sheth


4 Answers

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.

like image 142
lari Avatar answered Oct 20 '22 20:10

lari


Just a thought, careful while you are using BASEURL with AFNetworking. If you are supplying the exact endpoint url to AFNetworking it might fail.

For eg:

You have an endpoint URL = http://myweb.com/api.php

In the above case if you are using AFNetworking, the BASEURL != URL whereas it should be something like BASEURL=http://myweb.com/ and while calling for any request supply URL = api.php. AFNetworking appends the BASEURL + URL and creates a whole endpoint url.

Though the above answer might not be correct for your case. But in case it helps in taming other problem, or may be in your case too.

Thanks.

like image 29
iphonic Avatar answered Oct 20 '22 19:10

iphonic


The "Request failed: unacceptable content-type: application/soap+xml" error is generated by

- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                    data:(NSData *)data
                   error:(NSError * __autoreleasing *)error

method of AFHTTPResponseSerializer in case of unexpectable MIME type of response.

You can fix it with following code

self.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSSet *set = self.responseSerializer.acceptableContentTypes;
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"application/soap+xml"];

or modifying AFXMLDocumentResponseSerializer - add "application/soap+xml" to acceptable content types:

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml", @"application/soap+xml", nil];

    return self;
}

Following will not solve this problem I guess

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"];

There is not enough information to understand why do you have ""Request failed: internal server error (500)." I you will post correct and incorrect requests we will try to help you.

like image 1
Avt Avatar answered Oct 20 '22 20:10

Avt


Try this code :

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Accept"];
like image 1
Bikramjit Singh Avatar answered Oct 20 '22 20:10

Bikramjit Singh