Reputation: 3677
When ever i hit on my API too fetch some data it always gives me this Error.
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"
UserInfo={AFNetworkingOperationFailingURLResponseErrorKey= { URL: http://www.mydealdoc.com/api/v8/fencerecorddata } { status code: 500, headers { "Cache-Control" = "no-cache"; Connection = close; "Content-Type" = "text/html"; Date = "Sat, 24 Sep 2016 13:10:07 GMT"; Server = "Apache/2.4.7 (Ubuntu)"; "Set-Cookie" = "laravel_session=eyJpdiI6IjQ3SEtFTEt3TmN0dUFqMzZlMFB6OGZVNjdNS240NjdBVU9lSDhcLzNzeUVnPSIsInZhbHVlIjoicXlzbURmMEd5K0hlVEgwUTU0QmhjbFN0QlpNMFdXT0VGWmZrQm1jUXFDNlhVV05EK1wvTnpNUnA2WHBualpWUjRWZVkrSGRxSlBRaHpIS05MQit6SFdnPT0iLCJtYWMiOiIwYTlmYzUwNjBmNzgwYWE3MDg5MTMyMTlhN2MwYzQ5YTU3ZDAxMmQ4YThhMjU4MDFiNjAwYjYxYTQwMzdlMWQ3In0%3D; expires=Sat, 24-Sep-2016 15:10:07 GMT; Max-Age=7200; path=/; httponly"; "X-Frame-Options" = SAMEORIGIN; "X-Powered-By" = "PHP/5.5.9-1ubuntu4.9"; } }, NSLocalizedDescription=Request failed: internal server error (500), NSErrorFailingURLKey=http://www.mydealdoc.com/api/v8/fencerecorddata}
I have google for this question and they all replied with this solution that i should set response serializer to JSON. But i already have set it.
Here is my code.
-(void)userHasCheckThisStoreDealsWithStoreName:(NSArray *)data {
NSString *storeName = [data objectAtIndex:0];
NSString *branchName = [data objectAtIndex:1];
NSString *userID = [data objectAtIndex:2];
NSString *check = [data objectAtIndex:3];
NSLog(@"Store Name: %@",storeName);
NSDictionary *param = @{@"user_id":userID ,@"s_name":storeName , @"branch_name":branchName, @"enter_flag":check};
[BabyNetworkManager postWithUrlString:@"http://www.mydealdoc.com/api/v8/fencerecorddata" parameters:param success:^(id data){
NSError *error;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];;
NSLog(@"Dictionary = %@",responseDictionary);
if ([[responseDictionary objectForKey:@"status"] isEqualToString:@"success"]) {
}
else{
NSArray *arrayOfParamObjects = [NSArray arrayWithObjects: storeName,userID,branchName,check, nil];
[self performSelectorInBackground:@selector(userHasCheckThisStoreDealsWithStoreName:) withObject:arrayOfParamObjects];
}
} failure:^(NSError *error){
NSLog(@"Error %@",error);
}];
}
Please do read the comment i have mentioned in line number 3 of below function.
+(void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//
manager.requestSerializer = [AFJSONRequestSerializer serializer];// I have check this line by commenting and uncommenting in both state i get the same error
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
[manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
failure(error);
}];
}
Upvotes: 0
Views: 432
Reputation: 4579
HTTP 500 is the server error, I guess this error is caused by the invalid post param
you have sent to the server, and some exception occurred in server side.
Because I have tried your API in OC, I can get the correct JSON response. Here comes the sample code:
ApiClient.m
#import "ApiClient.h"
@implementation ApiClient
+ (instancetype)sharedClient
{
static ApiClient *_sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[ApiClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.mydealdoc.com"]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
});
return _sharedClient;
}
@end
ViewController.m
#import "ViewController.h"
#import "ApiClient.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *param = @{@"user_id":@"123" ,@"s_name":@"123" , @"branch_name":@"123", @"enter_flag":@"123"};
[[ApiClient sharedClient] POST:@"/api/v8/fencerecorddata" parameters:param constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@", [responseObject description]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", [error localizedDescription]);
}];
}
@end
Here is the Console log:
2016-09-25 11:14:01.588 StackoverflowTestOC[3314:41971] { status = success; }
Upvotes: 2