2

I am calling the REST service to post lat & lng, however The data is not getting send to server

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *crnLoc = [locations lastObject];
    self.locationDesc.text = [NSString  stringWithFormat:@"%@",crnLoc.description];
    NSLog(@"%@",crnLoc.description);
    NSURL *aUrl = [NSURL URLWithString:@"http://localhost/web/location/create.php?"];
    NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:aUrl];
    NSString *params = [[NSString alloc] initWithFormat:@"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
   [request setHTTPMethod:@"POST"];
   [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
   [[NSURLConnection alloc] initWithRequest:request delegate:self];
   NSLog(@"%@",request.URL);
}

Is something wrong here which I am doing in above code? I can post the data using REST Easy client (firefox add-ons).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Santosh
  • 202
  • 3
  • 16
  • what are the parameters and where are you appending here? – user3182143 Jun 10 '15 at 11:45
  • >> The parameters are @"latitude=%g&longitude=%g", .... at * param and sending at [request setHTTPBody:[params..... – Santosh Jun 10 '15 at 12:03
  • I wanted to pass the lat & lng to webservice so that the final url is - http://localhost/web/location/create.php?latitude=1111&longitude=22222 but somehow url is not getting constructed – Santosh Jun 10 '15 at 12:06
  • If you want them in the URL you need to add them to the location. Right now you are putting them in the body. Or if it indeed a post request, the data should be put in the body and the service should get the data from the body. – ophychius Jan 01 '17 at 10:01

1 Answers1

1

Use this code

CLLocation *crnLoc = [locations lastObject];
self.locationDesc.text = [NSString  stringWithFormat:@"%@",crnLoc.description];
NSLog(@"%@",crnLoc.description);
NSURL *aUrl = [NSURL URLWithString:@"http://localhost/web/location/create.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest
                            requestWithURL:aUrl];
NSString *params = [[NSString alloc] initWithFormat:@"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(con) 
{
   NSLog(@"Connection Successful");
} 
else 
{
   NSLog(@"Connection could not be made");
} 

For reference:

Sending an HTTP POST request on iOS

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Thanks for update but data is not getting sent to server. The parameter are still not appended to url. I printed the [param] and request.url 'NSLog(@"%@",params);' prints - latitude=-33.8634&longitude=151.211 and request.url - http://localhost/web/location/create.php?. – Santosh Jun 10 '15 at 12:49
  • The problem is http://localhost/web/location/create.php? because it gives Unable to connect.So give the correct URL.Otherwise it won't append. – user3182143 Jun 10 '15 at 12:56
  • Above if my answer is helpful for you,please tick and give me the mark. – user3182143 Jun 10 '15 at 12:58
  • No its connecting and i am getting "Connection Successful" Will defiantly give the mark. – Santosh Jun 10 '15 at 13:04
  • It turns out to the url parameter instead of sending the body:) finally I have to do the following to test the webservice using simulator. NSURL *aUrl = [NSURL URLWithString:@"http://localhost/web/location/create.php?latitude=222&longitude=111"]; – Santosh Jun 10 '15 at 13:43
  • Thank You for your response. – user3182143 Jun 10 '15 at 13:47