Skip to content

Instantly share code, notes, and snippets.

@mteece
Created July 28, 2014 18:25
Show Gist options
  • Save mteece/90d10aa9c2c0a19c8ccf to your computer and use it in GitHub Desktop.
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
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