30

been wrestling with this for some time. I am trying to access a REST api on my iphone and came across the ASIHTTP framework that would assist me. So i did something like

//call sites, so we can confirm username and password and site/sites
NSURL *url = [NSURL URLWithString: urlbase];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUsername:@"doronkatz%40xx.com" ];
[request setPassword:@"xxx"];

Where urlbase is a url to a REST site.

Now, a developer has told me there might be an issue or bug with this framework, and its not passing headers correctly. Is there another way of testing or accessing with authentication a network REST location?

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
Doz
  • 7,009
  • 12
  • 61
  • 69

5 Answers5

33

I would recommend checking out RestKit: http://restkit.org/ It provides an excellent API for accessing RESTful web services and representing the remote resources as local objects, including persisting them to Core Data. HTTP authentication is supported out of the box.

Blake Watters
  • 6,607
  • 1
  • 43
  • 33
  • 2
    in 0.20.0 how is Basic authentication performed? The only examples I can find relate to `RKClient` yet this has been removed in 0.20.0. Your help is very much appreciated. – Brett Ryan Dec 13 '12 at 04:51
10

I'm new to iOS development and I've been battling with some of the big frameworks listed on this page for the past month or so. It has been a nightmare. I'd honestly recommend you just stick to the basics and do it yourself using AFNetworking or Apple's own NSURLConnection.

One of the libraries listed is no longer maintained. Another underwent huge code-breaking API changes recently and now half of the tutorials describing its use no longer work. Others are massively bloated.

It's easier than you'd think. Some resources that helped me:

The examples on the AFNetworking homepage alone may get you 80% of the way there.

UPDATE: The Mantle Framework (open sourced by Github Inc.) is well-designed and easy to use. It handles object mapping: converting a JSON NSDictionary to your own custom Objective-C model classes. It handles default cases sensibly and it's pretty easy to roll your own value transformers, e.g. from string to NSURL or string to your custom enum.

bcattle
  • 12,115
  • 6
  • 62
  • 82
6

I have a couple of apps using a framework called Objective Resource which provides a wrapper for accessing remote REST based api's. It is aimed primarily at Ruby on Rails based applications so it's XML/JSON parsing may be tuned to handle some Rails defaults but it is worth looking at. It supports http basic authentication by default.

paulthenerd
  • 9,487
  • 2
  • 35
  • 29
2

Just stumbled on this question - you might find LRResty pretty interesting as it uses NSOperation, blocks etc., see: GitHub for source (MIT license). I'm experimenting with it now - it has a sample app too.

petert
  • 6,672
  • 3
  • 38
  • 46
1

I've used ASIHTTP in two apps so far and have had no problems.

Looks like you're doing HTTP Basic Auth with the remote site. Try hitting the same REST URL from a standard browser and pass the params you need down to it. It should prompt you for username/password. If it makes it through, then at least you know the server-side is set up to handle requests. If it doesn't, then you need to have a talk with the dev.

The next thing to try is put a Mac-based network sniffer and see what headers are going back and forth. Any of HTTPScoop, Wireshark, or Charles should work. Run the sniffer as a network proxy then run your app in the simulator and watch what goes across. Try it again with the browser. Once you see the differences, you can use the addRequestHeader method on ASIHTTPRequest to add any specific headers the server expects.

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
Ramin
  • 13,343
  • 3
  • 33
  • 35
  • Hi Ramin ive tried using the ASIHTTPRequest, even with the Twitter api to see if i can get a header thing to work, using addRequestHeader and using HTTPScoop but it does not show that headers are being passed to it. – Doz Oct 13 '09 at 08:10
  • Which headers are you looking for? And when you hit the same URL with the browser, are you getting what you're looking for in HTTPScoop? If you are getting headers from browser but not in ASIHTTP, then you might want to file a bug report with the developer. – Ramin Oct 13 '09 at 08:21
  • Well for either of the sites im working on, with HTTPSchool im getting a response saying authentication needed. With Twitter, i can put the username and password in URL file, but when i put it as addRequestHeader HTTPSchoop doesnt show that i had put any headers. Thats why im wondering there must be something with ASIHTTP. – Doz Oct 13 '09 at 11:26
  • ASIHTTP does support basic username/password authentication using the API you list. I don't think you need to add any extra headers unless it's a custom authentication scheme. I suggest you repost your question to their Google group for specific help: http://groups.google.com/group/asihttprequest – Ramin Oct 13 '09 at 20:06
  • 1
    Unfortunately, ASIHTTP is no longer being maintained, and the author recommends you use a different library. I don't think this means "don't use it", but it's something to consider. See << http://allseeing-i.com/[request_release]; >> (link includes the semicolon) – Greg M. Krsak Aug 16 '12 at 18:34