Search code examples
iosjsonnsurlconnectionnsurlnsurlconnectiondelegate

POST request not receiving all json


Little issue that's been bothering me. I've been making a POST request to my AWS RDB. The request should return a json output. The issue I'm having is that I'll receive bytes back, but sometimes it contains incomplete json, thus converting it to a dictionary won't work. Sometimes I also receive a null value for the nsdata received, but I can print out the length of the data. Any ideas? Here's my iOS code for requests:

#import "ServiceConnector.h"

@implementation ServiceConnector{
    NSMutableData *receivedData;
}

-(void)getTest{

    //Send to server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]];

   [request setHTTPMethod:@"GET"];

    //initialize an NSURLConnection  with the request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }

} 

-(void)postTest:(NSMutableArray *)carSearches{

    //build up the request that is to be sent to the server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]];

    [request setHTTPMethod:@"POST"];

    NSError *writeError = nil; 
    NSData *data = [NSJSONSerialization dataWithJSONObject:carSearches options:NSJSONWritingPrettyPrinted error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"JSON Output: %@", jsonString);

    [request setHTTPBody:data]; //set the data as the post body
    [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(!connection){
        NSLog(@"Connection Failed");
    }
}

#pragma mark - Data connection delegate -
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // executed when the connection receives data
    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
        [receivedData appendData:data];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //executed when the connection fails

    NSLog(@"Connection failed with error: %@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"Request Complete,recieved %lu bytes of data",(unsigned long)receivedData.length);

    NSString *tmp = [NSString stringWithUTF8String:[receivedData bytes]];
    NSLog(@"%@",tmp);

    NSError *error; 
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:[receivedData bytes] length:[receivedData length]] options:NSJSONReadingAllowFragments error:&error];

    [self.delegate requestReturnedData:dictionary];
}

Solution

  • In this section:

    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
        [receivedData appendData:data];
    }
    

    You are only appending data if the object hasn't been created yet. You want to append every time. That if statement should read like this:

    if(!receivedData){
        receivedData = [[NSMutableData alloc]init];
    }
    [receivedData appendData:data];