Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

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 ?

Upvotes: 0

Views: 870

Answers (3)

Usman Awan
Usman Awan

Reputation: 1258

The Webservice URL you are trying to hit is seem to be using SSL. You may need to put access token in request header in order to get the proper response from web service.

Please see the link for authentication: https://developers.geoloqi.com/api/authentication

Upvotes: 0

Bogdan
Bogdan

Reputation: 44556

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\" }";

Upvotes: 3

sergio
sergio

Reputation: 69047

It is not clear from your question what is your issue with that.

Anyway, are you doing also:

[m_URLConnection start];

after creating the connection?

Alternatively, you could create the connection using

[– initWithRequest:delegate:startImmediately:][1]

which allows you to specify that the loading of data shall start immediately.

Also consider using [+sendAsynchronousRequest:queue:completionHandler:][2] as a convenience constructor for your connection.

Upvotes: 0

Related Questions