I have a basic php script code that receives JSON POST and sends back a http status code.
PHP successfully receives JSON and sends http 200 status code but somehow AFNetworking gets Expected status code in (200-299), got 500
, in php script I define a 500 statuscode but I never use/send that status code, I have been looking to php script over and over again no it sends correct code 200
but AFNetworking receives it as 500
PHP:
//send delete request here
$query ="DELETE FROM OWN_EVENTS WHERE event_id IN ('$names')";
$result = $sql->query($query);
//printf("$query: %s
", $query);
//var_dump($query);
//printf("
");
if (!$result) {
//var_dump($result);
//printf("
");
//printf("Query failed: %s
", $mysqli->error);
sendResponse(417, json_encode("Query failed"));
exit;
}
sendResponse(200, json_encode("Event Deleted"));
IOS
NSError* error;
NSMutableDictionary *nameElements = [[NSMutableDictionary alloc] init];
[nameElements setObject:saveArray forKey:@"event_id"];
NSData *result =[NSJSONSerialization dataWithJSONObject:nameElements options:0 error:&error];
NSString *displayJson = [[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding];
NSLog(@"saveArray result %@",displayJson);
NSURL *url = [NSURL URLWithString:server];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// don't forget to set parameterEncoding!
httpClient.parameterEncoding = AFJSONParameterEncoding;
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:deleteEventInDatabase parameters:nil];
[request setHTTPBody:[displayJson dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue:@"ASIHTTPRequest" forHTTPHeaderField:@"User-Agent"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSLog(@"request %@",request);
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil];
operation.JSONReadingOptions = NSJSONReadingAllowFragments;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
// Print the response body in text
if (operation.response.statusCode == 200) {
NSLog(@"Events Sucessfully Deleted");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (operation.response.statusCode == 500) {
NSLog(@"Internal Server Error");
//remove this when u fix it
}
}];
[operation start];
ERROR:
Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x11c8c9d0 {NSLocalizedRecoverySuggestion="Event Deleted", AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://host.php>, NSErrorFailingURLKey=http://host.php, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa9aba80>
As you can see in error it says NSLocalizedRecoverySuggestion="Event Deleted"
so php really sends 200
What am I doing wrong?
500 is the HTTP status code for an internal server error. The problem is entirely in how your php script is responding to the request.