I'm attempting to make an iphone app that will interact with a particular JIRA server. I've got the following code to log in:
NSURL *url = [[NSURL alloc] initWithString:@"https://mycompany.atlassian.net/rest/auth/latest/session/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERROR: %@", error);
}
];
[operation start];
But it's giving me the following error having to do with Content-Type:
ERROR: Error Domain=AFNetworkingErrorDomain Code=-1011
"Request failed: unsupported media type (415)"
UserInfo=0x8cd6540
{
NSErrorFailingURLKey=https://mycompany.atlassian.net/rest/auth/latest/session/,
NSLocalizedDescription=Request failed: unsupported media type (415),
NSUnderlyingError=0x8c72e70
"Request failed: unacceptable content-type: text/html",
I'm not sure what the problem is. I found this question, which I thought might be a similar problem, but the answers say to either use the AFJSONRequestOperation
class (which I can't because I'm using AFNetworking version 2, which doesn't have that class), or to fix it on the server side (which I also can't for obvious reasons).
What can I fix this error when I can't fix the server side and I can't use AFJSONRequestOperation
?
stringWithFormat
(if password had certain characters in it, it would fail). Better to useNSDictionary *postDictionary = @{@"username":username,@"password":password};
and then useNSData *postBody = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:&error];
to create theNSData
forsetHTTPBody
. Or in AF 2.0 usePOST
andAFJSONRequestSerializer
(in AF 1.x useAFHTTPClient
withparameterEncoding
ofAFJSONParameterEncoding
), and it will convert the dictionary to JSON for you. – Edson