POST API调用与JSON正文有问题

时间:2014-08-23 16:05:51

标签: ios objective-c iphone rest post

我试图连接到API并且我一直收到错误消息,说我没有在通话中包含client_id。这是代码:

+ (void)connectWithUsername:(NSString *)username Password:(NSString *)password Type:(NSString *)type Email:(NSString *)email
{
    NSString *urlString = [kTestAPI stringByAppendingString:@"connect"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];

    NSDictionary *params = @{@"client_id":client,
                         @"secret":secret,
                         @"credentials":@{@"username":username,@"password":password},
                         @"type":type,
                         @"email":email};
    NSLog(@"PARAMS: %@", params);
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
    request.HTTPMethod = @"POST";
    request.HTTPBody = jsonData;

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSDictionary *output = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"SUCCESS: %@", output);
     }];
}

这是输出:

2014-08-23 11:52:53.131 Balance[11538:60b] PARAMS: {
    "client_id" = "test_id";
    credentials =     {
        password = "plaid_good";
        username = "plaid_test";
    };
    email = "test@plaid.com";
    secret = "test_secret";
    type = wells;
}

2014-08-23 11:52:53.554 Balance[11538:60b] SUCCESS: {
    code = 1100;
    message = "client_id missing";
    resolve = "Include your Client ID so we know who you are.";
}

最初,我认为这可能是因为我在NSLog时它会在引号中显示client_id,但在做了一些搜索后我发现显然这只是从NSLog调用的描述方法的一个函数,因为client_id有一个因此,它不被视为字母数字,因此会添加引号,因此我认为不应该影响JSON。所以,是的,我很难过,任何帮助都会受到赞赏:)

1 个答案:

答案 0 :(得分:3)

工作:curl -X POST tartan.plaid.com/connect \ -d client_id=test_id \ -d secret=test_secret \ -d credentials='{ "username":"plaid_test", "password":"plaid_good"}' \ -d type=wells \ -d email=test@plaid.com

请注意,在工作卷曲client_id中,secrettypeemail 是json的一部分,但是单独的POST项目。 JSOM项credentials有两个项目:usernamepassword

您需要在代码中执行相同的操作 client_idsecrettypeemailcredentials是POST变量。 'credentials'有一个JSON字符串作为它的值。