0

Folks,

in iOS, I want to send authentication for an API link after authentication I want to process another JSON Request API with Accept: application/json

How would I do something like that?

I tried this approach but it doesn't work:

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"URL for authentication"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    //[request setHTTPBody:data];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    [connection start];

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge previousFailureCount] == 0) {
        NSLog(@"received authentication challenge");
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"username"
                                                                    password:@"pass"
                                                                 persistence:NSURLCredentialPersistenceForSession];
        NSLog(@"credential created");
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];



        NSLog(@"responded to authentication challenge");

        // Second JSON Request API here


    }
    else {
        NSLog(@"previous authentication failure");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"%@",response);

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {


}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

See this link

https://checkout.hackathon.sla-alacrity.com/catalogs/443b70de5cd4429e43e9da9b87d6468058d0ae737fd6b8771

it is show

{ "error": { "message": "Invalid Accept header. Need to pass HTTP Header Accept: application/json", "type": "bad_request" } }

haz azezan
  • 27
  • 1
  • 10

1 Answers1

0

You can check this answer. I would suggest using AFNetworking too.

If you are using basic HTTP authentication I don't see a need to specifically request an authentication URL, you should simply request the JSON API in the first place. If you have the username and password beforehand you should set them in the header before making the request:

#import "NSData+Base64.h"
...
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"username", @"pass"];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];

The code above has the NSData+Base64.h dependency, you will have to add it to your project, you can download it in: NSData+Base64 class and header

In the case you are prompting the user for credentials (which is not shown in your code) and you do need to set the credentials in the connection:didReceiveAuthenticationChallenge the same still applies you can just use the URL you are trying to create inside the authentication challenge method:

// Second JSON Request API here
Community
  • 1
  • 1
rvil
  • 136
  • 1
  • 4
  • I copied the answer from the post I told you, and it has an AFNetworking dependency. I updated the code which also has a dependency but I provided the link to download it – rvil Nov 20 '13 at 20:28