1

Calling REST web services is simple enough - I use the native NSURLConnection class and its delegate methods:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"webServiceURL"]];

    [request setHTTPMethod:@"GET"];

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

But how do I pass object parameters to the call? For instance, if the web service expects an object UserInfo { name:string, age:int}.

Now this call only has two parameters - I'm also thinking of possible cases where I might have to pass many more in the request.

What's the right way to do this? I've heard of RestKit and plan to try it soon, but is there any good way without third party libraries?

2
  • Parameters in the URL or the request body? Commented Jul 31, 2013 at 20:12
  • In the request body. In the URL would be ok, but if I have to pass 10 parameters, the url length gets too long. I tried [request setValue:@"user1" forHTTPHeaderField:@"username"]; but the REST Web API doesn't recognize the parameters passed. Commented Jul 31, 2013 at 20:14

1 Answer 1

2

Usually when doing a GET you would add the parameters to the URL. To do that, just add the query string to the end of the string used to generate the URL.

If you were doing a POST then you would usually add the parameters to the request body. This is done by converting the parameters to NSData and calling setHTTPBody: on the request.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't think it was that simple. Thanks! The GET request does work properly with query string params.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.