3

Starting to look into iOS and am in need of Restful networking so I'm looking at AFNetworking. I've followed some good examples and I think I understand how to use it but the request that gets built does not have the expected URL. In AFHTTPClient requestWithMethod path parameters method,

po urlBase
$0 = 0x003b23b8 https://mysite.com/developer/api
(lldb) po url
$1 = 0x1eddf140 /users/current -- https://mysite.com/developer/api/
(lldb) po request
$2 = 0x1ed50580 <NSMutableURLRequest https://mysite.com/users/current>

The developer section is getting stripped for some reason.

I've seen examples where the relative path string passed into the getPath method has a leading slash and therefore one would expect the base URL to not have a trailing slash but I've seen examples where folks put a slash in both.

My base URL var is

NSString *const ServiceUrlBaseProduction  = @"https://mysite.com/developer/api/";

and the relative path to get current user is

@"/users/current";
1
  • Why are you including the leading slash for a relative path? Apr 5, 2013 at 14:57

2 Answers 2

5

This is because of AFHTTPSessionManager's interpretation of URLs. In AFHTTPSessionManager.h here there is an explanation of how to write your URLs correctly. I believe you will want to get the current user with the path users/current instead (without the leading slash)

4
  • yeah, so even if i leave the trailing slash off the base url, the url will have it anyway, and if i leave the leading slash off my relative path, the url build appropriately.
    – topwik
    Apr 5, 2013 at 14:58
  • Exactly. I agree that it's confusing behavior Apr 5, 2013 at 15:29
  • The link is now a 404, and I think AFHTTPClient.h is no longer there. An explanation of how to write URLs is found in the source of AFHTTPSessionManager.h though Aug 1, 2015 at 9:18
  • I've updated the link and pointed it to the most recent commit so the link won't break again, although it might become outdated. Aug 2, 2015 at 18:16
0

try using NSURL:

NSURL *url = [NSURL URLWithString:@"https://mysite.com/developer/api/"];

Then:

[NSURL URLWithString:@"users/current" relativeToURL:url]; 
1
  • i'm trying to build a client that inherits from AFHTTPClient. My init initializes itself using NSURL. [self = [super initWithBaseURL:[NSURL URLwithString:[[self class] urlBase]]]...
    – topwik
    Apr 5, 2013 at 15:00

Your Answer

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

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