1

我有这个示例调用:

curl -i https://sandbox.espago.com/api/tokens -u publickey: -d "card[first_name]=Jan" -d "card[last_name]=Kowalski" -d "card[number]=4242424242424242" -d "card[verification_value]=123" -d "card[year]=2014" -d "card[month]=02" 

我需要发出 POST (AFNetworking 1.3) 请求,我试过这样:

[self.client setAuthorizationHeaderWithUsername:self.publicKey password:@""];
NSDictionary *parameters = (@{
                                  @"card[first_name]" : firstName,
                                  @"card[last_name]" : lastName,
                                  @"card[number]" : cardNumber,
                                  @"card[verification_value]" : verificationCode,
                                  @"card[year]" : @(year),
                                  @"card[month]" : monthString,
                                  });
    NSString *path = [NSString stringWithFormat:@"%@/tokens", ZWEspagoURL];
    [self.client postPath:path
               parameters:parameters
                  success:^(AFHTTPRequestOperation *operation, id responseObject) {

                  }
                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                      if(completion) completion(nil, error);
                  }];

但不幸的是,我得到了 401 或 422 HTTP 状态代码......

这个 curl 命令是 100% 好的,工作没有问题

4

1 回答 1

1

所以我最终决定使用普通的 NSURLRequest 并放弃 AFNetworking 来做这么简单的事情,这里是如何将 curl 的 -d 选项转换为 NSURLRequest 模式的代码:

NSDictionary *parameters = (@{
                              @"card[first_name]" : firstName,
                              @"card[last_name]" : lastName,
                              @"card[number]" : [cardNumber stringByReplacingOccurrencesOfString:@" " withString:@""],
                              @"card[verification_value]" : verificationCode,
                              @"card[year]" : @(year),
                              @"card[month]" : monthString,
                              });
NSMutableArray *httpDataComponents = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in parameters) {
    [httpDataComponents addObject:[NSString stringWithFormat:@"%@=%@", key, parameters[key]]];
}
NSString *httpDataString = [httpDataComponents componentsJoinedByString:@"&"];
NSLog(@"http data string %@", httpDataString);
NSString *path = [NSString stringWithFormat:@"%@/tokens", ZWEspagoURL];

NSURL *url = [NSURL URLWithString:path];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self publicKey], @""];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setHTTPBody: [httpDataString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setValue:[NSString stringWithFormat:@"%d", httpDataString.length] forHTTPHeaderField:@"Content-Length"];
于 2013-10-22T08:59:57.603 回答