1

I tried to search around but can't find the exact answer.

My friend implements the website to return data in JSON by using cURL as below

curl http://example.com//api/v1/opens_set_current_position -d '{"latitude":"53.041", "longitude":"-2.90545", "radius":"100000"}' -X GET -H "Content-type: application/json"

I tried to write code to make my iOS app to retrieve data. But I can't. I always get error the data can't even load via NSURLConnection. Here is my code:

NSString *requestString = [NSString stringWithFormat:
                         @"http://example.com//api/v1/opens_set_current_position"];

NSURL *url = [NSURL URLWithString:requestString];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"GET"];

[req addValue:@"application/json" forHTTPHeaderField:@"Content-type"];

NSString *dataString = @"{\"latitude\":\"53.041\", \"longitude\":\"-2.90545\", \"radius\":\"100000\"}";

NSData *requestData = [NSData dataWithBytes:[dataString UTF8String] length:[dataString length]];

[req setHTTPBody:requestData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&theResponse error:&theError];

The data didn't come at all. I get this error message:

The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)

Can somebody help?

1

1 Answer 1

3

This is because you are passing a body in a GET request. You want a POST request instead.

From the curl manpage:

-d/--data <data>
(HTTP)  Sends  the  specified data in a POST request to the HTTP server,
in the same way that a browser does when a user has filled in an HTML form
and presses the submit button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-urlencoded.
2
  • Thanks for prompt reply. When if I set the httpMethod to "POST",the (kCFErrorDomainCFNetwork error 303.)" disappeared but I still not able to get what I wanted ,the data return with "500" code. It seems to be the problem with the server side. Thanks anyway. Mar 15, 2013 at 16:42
  • 2
    @KongHantrakool I think I've answered the question you asked.
    – trojanfoe
    Mar 15, 2013 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.