Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge delay on DELETE requests with 204 response and no content in Objective-C

I'm having problem with iOS app (I'm not iOS developer, I'm responsible for API that that app uses) and DELETE requests.

Api is using 204 responses with no content for DELETE requests and that have worked fine for all the client applications so far, without any problems.

The issue is that when using NSUrlConnection all those DELETE requests either are processed for more than 60 seconds or fail because of timeout.

This behaviour is only visible in out iOS implementations, other clients are getting response for exactly same request in less than 100ms.

Is this common behaviour? Do anyone know any fix for that that hopefully doesn't require API rebuild?

Code below was created just to emulate this behaviour and replicate problem on API development team side, but problem is the same (Access token obscured of course, authentication works fine):

//
//  ViewController.m
//  NoteablesTest

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *loadingLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_loadingLabel setHidden:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)didButtonTouched:(id)sender {
    NSString *url = @"https://api.noteables.com/editing-session/c180af93-ad3a-4751-a96a-dc47ff7732d4";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

    [request setHTTPMethod:@"DELETE"];

    [request setValue:@"Bearer XXXX" forHTTPHeaderField:@"Authorization"];

    [request setValue:@"Noteables/1.0 (iPhone; iOS 8.1.3; Scale/2.00)" forHTTPHeaderField:@"User-Agent"];

    [request setValue:@"application/vnd.api.v1+json" forHTTPHeaderField:@"Content-Type"];

    [request setValue:@"en;q=1" forHTTPHeaderField:@"Accept-Language"];

    NSDate *start = [NSDate date];

    [_loadingLabel setHidden:false];

    [NSURLConnection  sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        double timePassed = [start timeIntervalSinceNow];
        NSString *message = [NSString stringWithFormat:@"Elapsed: %f seconds", timePassed];
        [_loadingLabel setHidden:true];


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result!" message: message  delegate:self cancelButtonTitle:@"Am I satisfied with this?" otherButtonTitles:nil, nil];

        [alert show];

        NSLog(@"%@", response);
    }];


}

@end
like image 512
Adam Avatar asked Feb 23 '15 21:02

Adam


People also ask

How do I fix 204 No Content response?

By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.

What does 204 no content mean?

5 204 No Content. The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

Why do I get error 204?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

What is the difference between 200 and 204 status code?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.


2 Answers

Try setting your content-length header to 0.

like image 192
Aaron Brager Avatar answered Oct 23 '22 18:10

Aaron Brager


Any HTTP request sent from an iOS device that returns both a 204 status and a non-empty response (indicated by a content-length header with any value but 0) will cause the connection to run into a timeout on the device. iOS receives the 204 response, then discards the content, then reads the header and then mistakingly waits for a non-empty response to arrive.

Heads-up: the iOS simulator will NOT run into this problem. This seems to be a (real) device-only issue.

Without being able to inspect the code on the iOS client side of things, it has been suggested that this is a unintentional handling error on the HTTP request side, which means the implementation is not standard-conforming. A 204 response should ignore any content, as it is the "no content" response.

like image 39
arvidkahl Avatar answered Oct 23 '22 18:10

arvidkahl