1

I've got this sample call:

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" 

I need to make POST (AFNetworking 1.3) request, i tried this way:

[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);
                  }];

But unfortunately i'm getting 401 or 422 HTTP Status codes ...

This curl command is 100% ok, working without problem

Mapedd
  • 702
  • 7
  • 16
  • Why don't you log out your request and check it against the curl request and see what is different? – Tim Oct 16 '13 at 12:31
  • do you know how can i do it? – Mapedd Oct 16 '13 at 12:33
  • NSLog(@"%@", operation.request); inside the completion blocks – Tim Oct 16 '13 at 12:44
  • This is what i'm getting, now very usefull: { URL: https://sandbox.espago.com/api/tokens } – Mapedd Oct 16 '13 at 12:59
  • See this question for how to print the different properties of NSMutableURLRequest object, currently you're just getting the memory address http://stackoverflow.com/questions/5787010/how-to-print-nsmutableurlrequest – Tim Oct 16 '13 at 13:21

1 Answers1

1

So i finally decided to do it with the plain NSURLRequest and ditch AFNetworking for so simple thing, here is the code of how to convert curl's -d options to NSURLRequest schema:

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"];
Mapedd
  • 702
  • 7
  • 16