Example 1 – pass data using Header fields
On the server side, the receiving script will strip the information from the request object and use the data, query the database, and return it.
On our iOS client side, we create a NSURL object that takes in the string of the web api address that we want to hit.
For example in our case it would be
http://104.167.105.220:8080/users/(some email)
NSString –> NSURL –> NSMutableURLRequest
- So first, we assemble a string to satisfy the HTTP url.
- Then throw it in a NSURL.
- Create a NSMutableURLRequest object, and init it with our NSURL.
- Manipulate the request object with http header field/value combinations.
- set the HTTP VERB
NSMutableURLRequest object (iOS)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#define SERVER_IP_PORT @"104.167.105.220:8080" NSString * urlStr = [NSString stringWithFormat:@"http://%@/users/%@", SERVER_IP_PORT, self.email]; NSURL *url = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request addValue:self.email forHTTPHeaderField:@"x-access-email"]; [request addValue:self.auth_token forHTTPHeaderField:@"x-access-token"]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; |
NOTE:
The key/value you assign in your NSMutableURLRequest object will be read on the server side (node) on the request object’s header’s properties.
Reading values for HTTP Header fields (node)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
apiRoutes.use(function(req, res, next) { console.log('------apiRoutes.use function to verify token------------'); //X-ACCESS-TOKEN is the key, we need to give it value of token //so it can authenticate us // check header or url parameters or post parameters for token var token = req.headers['x-access-token']; var email = req.headers['x-access-email']; console.log('value of email: ' + email); console.log('value of token: ' + token); |
After you have set up your URL Request object, you insert it into a NSURLConnection, and make it run.
NSURLConnection object (iOS)
1 2 3 4 5 6 7 8 9 10 11 |
/* create the connection */ self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (self.connection) { //NSLog(@"GetUserInfoConnection---------> CONNECTION address: %p", self.connection); self.buffer = [NSMutableData data]; /* initialize the buffer */ //NSLog(@"GetUserInfoConnection---------> BUFFER address: %p", self.buffer); [self.connection start]; /* start the request */ self.completed = getUserBlk; } |
Full Source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
@interface GetUserInfoConnection () { } //block that gets retained when posting to WS, then used when the connection is finished @property (nonatomic, copy) OnGetUserFromCloud completed; @property(nonatomic, copy) NSString * auth_token; @property(nonatomic, copy) NSString * email; @property(nonatomic, strong) NSArray * trustedHosts; @end @implementation GetUserInfoConnection #pragma mark - ------------- LifeCycle ------------- -(instancetype)initWithEmail:(NSString*)email andToken:(NSString*)token { if(self=[super init]) { //self.trustedHosts = [NSArray arrayWithObjects:@"cloudatcost.mesotech.ca", nil]; //UserDB * userDB = [[UserDB alloc] init]; //self.auth_token = [userDB getAuthTokenByEmail:email]; self.auth_token = token; self.email = email; //NSLog(@"************ GetUserInfoConnection created, address: %p", self); } return self; } -(void)getUserInfo:(OnGetUserFromCloud)getUserBlk { NSString * urlStr = [NSString stringWithFormat:@"http://%@/users/%@", SERVER_IP_PORT, self.email]; NSURL *url = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request addValue:self.email forHTTPHeaderField:@"x-access-email"]; [request addValue:self.auth_token forHTTPHeaderField:@"x-access-token"]; [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"GET"]; /* create the connection */ self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (self.connection) { //NSLog(@"GetUserInfoConnection---------> CONNECTION address: %p", self.connection); self.buffer = [NSMutableData data]; /* initialize the buffer */ //NSLog(@"GetUserInfoConnection---------> BUFFER address: %p", self.buffer); [self.connection start]; /* start the request */ self.completed = getUserBlk; } } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self showMessageBox:@"error" andMessage:[error localizedDescription] andCancelTitle:@"ok"]; self.completed( nil, nil, [error localizedDescription]); } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSError *error = nil; NSDictionary* json = [NSJSONSerialization JSONObjectWithData: self.buffer options: kNilOptions error: &error]; //NSLog(@"%@", json); if(error) { self.completed( nil, nil, [error localizedDescription]); //if url is wrong [self showMessageBox:@"error" andMessage:[error localizedDescription] andCancelTitle:@"ok"]; } else { //success! self.completed(self.email, json, nil); } } @end |