0

I'm trying to make a POST request using NSURLConnection. I use Charles to debug and Charles every time says that the method is GET. I've tried all different ways and can't get it to work. I am NOT using JSON.

-(void)getList
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"http://example.com/api/getList"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString *radius = @"15";
    NSString *latitude = @"-117.820833";
    NSString *longitude = @"34.001667";

    NSString *parameters = [NSString stringWithFormat:@"longitude=%@&latitude=%@&radius=%@", longitude,latitude, radius];
    NSLog(@"PARAMS = %@", parameters);

    NSData *data = [parameters dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];

    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *responseString = [[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];

    NSLog(@"RESULT = %@", responseString);


}

Does anybody know what am I doing wrong? When I access my web service it seems like I'm not posting anything. I'm getting empty response.

Please help with any ideas. I pretty much have to make a simple POST request. Maybe someone can help me debug this better.

SasaT
  • 731
  • 6
  • 19
  • Is there any authorisation requirement on the server? Is your request being redirected? – Wain Jun 27 '13 at 19:19
  • Are you possibly calling this url as a GET somewhere else in the app? It might be caching the response for a request to that url and as such ignoring your POST altogether...Maybe give `request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;` a shot? – Stakenborg Jun 27 '13 at 19:26
  • That's a POST request if I ever saw one. The problem lies elsewhere. – Marcus Adams Jun 27 '13 at 19:40
  • @Wain Please post the same answer so I can accept it as right answer. My request is being redirected and that was the problem. Thank you a lot Wain. – SasaT Jun 27 '13 at 20:43

1 Answers1

0

If the server is redirecting your request for some reason (perhaps authentication) then the POST information can get lost.

Wain
  • 118,658
  • 15
  • 128
  • 151