Post JSON Body to API com Basic HTTP Authentication usando Objective C e AFNetworking 2.0

Certifique-se de verificar as diferentes opções NSURLCredentialPersistence – e nunca escreva uma senha no código.

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];