Created
July 28, 2014 18:25
-
-
Save mteece/90d10aa9c2c0a19c8ccf to your computer and use it in GitHub Desktop.
Post JSON Body to API with Basic HTTP Authentication using Objective-C and AFNetworking 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *URLString = @"http://example.com/path"; | |
NSDictionary *parameters = @{ }; | |
NSURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
operation.credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistencePermanent]; | |
operation.responseSerializer = [AFJSONResponseSerializer serializer]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSLog(@"JSON: %@", responseObject); | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"Error: %@", error); | |
}]; | |
[[NSOperationQueue mainQueue] addOperation:operation]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment