为什么API拒绝我的JSON?

时间:2014-03-18 15:39:55

标签: ios objective-c json nsjsonserialization

当我使用byte char []发送请求时,我可以通过JSON向API添加一个对象,但是当我将NSDictionary转换为NSData并发送它时它不起作用。这里的问题是什么?

这是在它工作的时候。

// Request

NSURL* URL = [NSURL URLWithString:@"SOME URL"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
request.timeoutInterval = 30.000000;

// Headers

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

// Body

const char bytes[98] = "{\n\t\"user\" :\n\t{\n\t\t\"email\" : \"test@gmail.com\",\n\t\t\"username\" : \"example\",\n\t\t\"password\" : \"cryptx\"\n\t}\n}";
request.HTTPBody = [NSData dataWithBytes:bytes length:98];

// Connection

NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate:nil];
[connection start];

这是什么时候不起作用:

    - (void)addUser:(User *)user
{
    NSURL* URL = [NSURL URLWithString:@"API_URL"];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
    request.HTTPMethod = @"POST";
    request.timeoutInterval = 30.000000;
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSData *data = [self createJSONWithUsername:@"X" andEmail:@"x@X.com" andPassword:@"pass"];
    [request setHTTPBody:data];
    NSLog(@"%@", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
}

- (NSData *)createJSONWithUsername:(NSString *)username andEmail:(NSString *)email andPassword:(NSString *)password
{
    NSArray *objects = [NSArray arrayWithObjects:password, email, username, nil];
    NSArray *keys = [NSArray arrayWithObjects:@"password", @"email", @"username", nil];
    NSDictionary *userDataDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    NSDictionary *userDictionary = [NSDictionary dictionaryWithObjectsAndKeys:userDataDictionary, @"user", nil];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error:nil];
    return jsonData;
}

第二个不添加用户而第二个用户添加。

修改

JSON BEING SENT即jsonString变量

{
  "user" : {
    "email" : "x@X.com",
    "username" : "X",
    "password" : "pass"
  }
}

1 个答案:

答案 0 :(得分:1)

我就是这样做的。它几乎就是我在一些应用程序中使用的内容。我根据您提供的代码格式化了......

- (void)addUser:(NSDictionary *)sentuserdetails {
  NSURL *url = [NSURL URLWithString:@"SOME URL"];
  NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
  request.HTTPMethod = @"POST";
  request.timeoutInterval = 30.000000;
  [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sentuserdetails      options:NSJSONWritingPrettyPrinted error:nil]; //don't set error to nil, handle the error
  [request setHTTPBody:jsonData];
  NSURLConnection *connection = [NSURLConnection connectionWithRequest:request           delegate:self];
  [connection start];
}