Search code examples
iphonejsonpostnsmutableurlrequest

Sending JSON Request to server?


I have following JSON for sending request on server

{
    "name":"Home",
    "latitude":"45.5037078163837",
    "longitude":"-122.622699737549",
    "radius":"240"
}

and URL of for request is

  URL: https://api.geoloqi.com/1/place/create 

I am making request like this,

    NSString *urlString= @"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";

jsonRequest = [self JSONString:jsonRequest];

NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [requestData length]];

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];

NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];

What is wrong with this request ?


Solution

  • Your JSON string seems to be missing 2 quotes after the longitude and radius values:

    Change

    NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";

    To this

    NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\",\"radius\":\"500\" }";