title: iOS: Getting 415(unsupported media type) error while consuming RESTFUL web service using NSUrlConnection
body: <p>I am trying to a restful web service in iOS application using NSUrlConnection. I am using below code to format the json string and then send over to a restful web service. But I am getting '415(unsupported media type)' response in connectionReceiveResponse: method. Please tell me where I am going wrong here.</p>
  
  <p>NSURL *urlLoginAuthentication= [NSURL URLWithString:@"<a href="http://www.loginService/mp/api" rel="nofollow">http://www.loginService/mp/api</a>"];</p>
  
  <pre><code>NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlLoginAuthentication];
  [request setHTTPMethod:@"POST"];
  
  NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] initWithObjects:[NSArray arrayWithObjects:self.txtUserName.text, self.txtPassWord.text,nil] forKeys:[NSArray arrayWithObjects:@"username",@"password", nil]];
  
  NSLog(@"Dict: %@",jsonDict);
  
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
  NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  NSLog(@"JSON String: %@",jsonString);
  
  [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
  [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"contentType"];
  
  [request setValue:@"json" forHTTPHeaderField:@"dataType"];
  [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
  [request setHTTPBody: jsonData];
  NSLog(@"jsonData: %@",jsonData);
  
  self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
  </code></pre>