Error 400 when using JSON POST method from iOS 5 to WCF service

Discussion in 'iOS Programming' started by aceswildab1, Jun 29, 2012.

  1. aceswildab1, Jun 29, 2012
    Last edited by a moderator: Jun 29, 2012

    aceswildab1 macrumors newbie

    Jun 29, 2012
    0 down vote favorite

    The get works fine and able to post to the database with a test winform app but getting a error 400 from the IIS log when trying to do a POST from iOS app. This is the IIS log 2012-06-28 12:13:39 POST /JsonWcfService/GetEmployees.svc/json/updateuser - 58129 - WcfTest/1.0+CFNetwork/548.0.3+Darwin/11.4.0 400 0 0 4163 I think there is something wrong with the format I'm sending to the WCF service but I'm very new to iOS Dev.

    This is the WCF service code
        [WebInvoke(Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "json/updateuser")]
         Employee PostEmp(Employee emp);    
    public class Employee
        public string firstname { get; set; }
        public string lastname { get; set; }
        public decimal salary { get; set; }
        public int idkey { get; set; }
        public Employee()
    public Employee GetEmp(int IDKey)
        Employee emp = new Employee();
        using (EmpDBEntities empContext = new EmpDBEntities())
            var j = (from t in empContext.EMPKeys where t.IDKey == IDKey select t).FirstOrDefault();
            emp = (new Employee(j.FirstName, j.LastName, j.Salary, j.IDKey));
            return emp;
    xcode was taken from another posting on
    NSArray *keys = [NSArray arrayWithObjects:@"idkey", @"firstname", @"lastname",@"salary", nil];
    NSArray *objects = [NSArray arrayWithObjects:@"1", @"jim", @"jones", @"450",nil];
    NSData *__jsonData = nil;
    NSString *__jsonString = nil;
    NSURL *url = [NSURL
    URLWithString:@"http://<ip address>/JsonWcfService/GetEmployees.svc/json/updateuser"];
    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    if([NSJSONSerialization isValidJSONObject:jsonDictionary])
        __jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
        __jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
    // Be sure to properly escape your url string.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: __jsonData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];
    NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
    if (errorReturned) {
        // Handle error.
        NSError *jsonParsingError = nil;
        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; }
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Glancing at your code I don't see a problem. You might want to get a packet trace so you can see exactly what's going over the wire.

    Go to the Documentation tab in the Organizer window and type in:

    packet trace

    There are a number of ways to do that. Usually you see the problem when you look at that level.

Share This Page