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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top