-1

In my current project I need to call a REST based web service, my colleague created a simple web service(nodeJS & express framework) which return data in json format. I was successful calling it using NSUrlConnection. Now my colleague applied authentication to service, First I tried using NSUrlConnection then AFNetworking then RestKit but didnt succeed.

Searching tutorial of RestKit are old(of restkit version 0.10) or no authentication tutorial available, All I want to do is call a simple REST based web service with credentials. Need help struggling for two days.

S.J
  • 3,063
  • 3
  • 33
  • 66
  • check this link hope it will help you http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – Exploring Mar 22 '13 at 11:14
  • @sanjitshaw I need POST based... – S.J Mar 22 '13 at 11:23
  • When you say AFNetworking did not succeed, how were you using it and what problems did you encounter? What type of authentication did your friend put in? – Frank C. Mar 22 '13 at 11:54

4 Answers4

1

Obviously this has already been answered for Apple's native HTTP methods. I'm not sure what issues you ran into with AFNetworking but in AFHTTPClient here you can obviously call either one of

- (void)setAuthorizationHeaderWithUsername:(NSString *)username
                                  password:(NSString *)password;

- (void)setAuthorizationHeaderWithToken:(NSString *)token;

Depending on your web service. Since RestKit is built on top of AFNetworking RKObjectManager has an httpClient property that is an instance of AFHTTPClient show you can call the methods above on it as well (from here).

Then you can call

- (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

from here.

Keith Smiley
  • 61,481
  • 12
  • 97
  • 110
1

To handle a GET/POST request/response with RestKit 0.20 you can follow below sequence :-

At very first configure RKObjectManager with the base server URL :-

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseUrl];
[manager.HTTPClient setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
manager.requestSerializationMIMEType = @"application/json";

Since RestKit always deal with objects for request and response, you must have to create an object having all the parameters that you expect in response.

@interface AuthenticationRequest : NSObject
@property (nonatomic, copy) NSNumber *userName;
@property (nonatomic, copy) NSString *password;
@end

@interface AuthenticationResponse : NSObject
@property (nonatomic, copy) NSNumber *token;
@property (nonatomic, copy) NSString *expiryDate;
@property (nonatomic, copy) NSString *userId;
@end

Then configure request and response mapping for the instance variables in your local objects with the keys in server JSON response.

NOTE: configure request mapping only in case of POST or PUT request.

RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[AuthenticationRequest class]];
[requestMapping addAttributeMappingsFromDictionary:@{
                @"userName": @"userName",
                @"password" : @"password",
            }];


RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AuthenticationResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{
                @"TOKEN": @"token",
                @"expiryDate" : @"expiryDate",
                @"USERID": @"userId"
            }];

Then create a response descriptor that will perform mapping of server JSON object with your local object based on the pathPattern value that you passed it.

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping  objectClass:[AuthenticationResponse class] rootKeyPath:nil]
[manager addRequestDescriptor:requestDescriptor];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:<rest of the path excluding the baseURL> keyPath:nil statusCodes:nil];
    [manager addResponseDescriptor:responseDescriptor];

Now perform GET request on server in following way :-

[manager getObjectsAtPath:(NSString *)<rest of the path excluding the baseURL>              parameters:(NSDictionary *)parameters
                 success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                 failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure];

or perform a POST request on server in following way :-

[manager postObject:AuthenticationRequest
              path:<rest of the path excluding the baseURL>
           success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
           failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure];

the success and failure blocks will hold your handling for the response.

For more help you can refer following link from RestKit :-

https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md
Anshul Sharma
  • 166
  • 1
  • 5
0

In NSURLConnectionDelegate

  • (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

method will be there it will help you to authenticate.

Rafeek
  • 523
  • 3
  • 16
0

credential = [NSURLCredential credentialWithUser:userName password:passWord persistence:NSURLCredentialPersistenceNone];

[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

Rafeek
  • 523
  • 3
  • 16