0

I'm new to using HTTP, as I have traditionally been a front-end developer only, but with my current contract I'm being asked to use a REST API to pull data from a server. I need to authenticate myself in the HTTP header with an API Key and API User Name and I'm being asked to do so in the "token" header according to the API documentation.

Can I get any help with how I would format an NSURLRequest to accomplish this task? I'm completely lost here.

Here is the specific part of the API documentation I'm referencing:

REST-APIUser—Token

The APIKey and APIUserName associated with the account have to be set to “REST-APIUser--Token” in Base64String Format in the token header as illustrated below: Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}",APIKey,APIUserName)))

Where,

APIKey – The Unique key associated with an API account

APIUserName – The UserName associated with an API account

APP-User-ID

ID of the currently logged in User has to be set to “APP-User-ID” in Base64String Format in the header. Convert.ToBase64String(Encoding.UTF8.GetBytes(AppUserID))

Where,

AppUserID – The User ID associated with the API application user

I am in possession of an {APIKey}, {APIUserName}, and an {AppUserID}.

amarkon
  • 341
  • 1
  • 6
  • 17

2 Answers2

3

See Add HTTP Header to NSURLRequest

/* Create request variable containing our immutable request
 * This could also be a paramter of your method */
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];

// Create a mutable copy of the immutable request and add more headers
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:@"__usersKey__" forHTTPHeaderField:@"token"];

// Now set our request variable with an (immutable) copy of the altered request
request = [mutableRequest copy];

// Log the output to make sure our new headers are there    
NSLog(@"%@", request.allHTTPHeaderFields);

Note that in case you meant HTTPS, the connection is the one that requires authentication, not the requests; though you don't seem to use HTTPS.

NSString* AppUserId = Convert.ToBase64String(Encoding.UTF8.GetBytes(AppUserID)) //Or whatever the function is that you need, this doesn't look like Objective-C.

Look at How to Base64 encoding on the iPhone or the likes for base64. This post suggest https://github.com/nicklockwood/Base64/

if using that library, you need something like the function

- (NSString *)base64EncodedString;

And pass it the information like it is described in your API documentation. We can't help you more than that since we don't have all your information.

For example, you probably want:

 NSString *token = [NSString stringWithFormat@"%@:%@",APIKey,APIUserName]
 NSString *token64 = [token base64EncodedString]; //Assuming that's how you call the library's function. I have never used it so I don't know if it modifies NSString or what. You can always write a function for this part.

[mutableRequest addValue:@"APIUser--Token" forHTTPHeaderField:token64]; //Not sure what the API says the value should be, formatting isn't clear

//Follow the same lines for NSString *AppUserId
[mutableRequest addValue:@"APP-User-ID" forHTTPHeaderField:AppUserId];

Just follow the API from there to do the token in the same way (generate the token in the same way).

Community
  • 1
  • 1
  • Can you check my updated question and make sure your answer still meets the API specification? Thanks – amarkon Dec 12 '13 at 01:06
  • I seem to think it does, could you be more specific where you think it doesn't? Or is it that you have a second question which is how to create the strings that will be passed to "forHTTPHeaderField:"? – Pierre-Francoys Brousseau Dec 12 '13 at 01:13
  • I am unsure about creating the strings that will be passed to "forHTTPHeaderField:". – amarkon Dec 12 '13 at 01:22
0

Start with NSMutableURLRequest and use the addValue:forHTTPHeaderField: or setAllHTTPHeaderFields: method to add the fields against the header names shown in the documentation.

Wain
  • 118,658
  • 15
  • 128
  • 151