3

I have a web service (which uses CakePHP) that I am posting string data to successfully with a Mac OS X app I'm working on.

Initially, I was given some advice that I would need to create a Base 64 encoded string to pass over a PNG imageto CakePHP. So my plan was to simply generate a URL encoded string (see code sample below). To do that, I've incorporated a third-party class which handles the base64 encode and decode.

However, it seems like the CakePHP update() function is removing some characters from that base 64 encoded string:

Original string may contain something like:
5nd/9ne/+zuPelWPFYtGrRpde2tlK3eXwv/

But the one that is actually stored and eventually retrieved may be:
5nd/9ne/ zuPelWPFYtGrRpde2tlK3eXwv/

Weird, right? I'm working on very limited knowledge of how CakePHP works, so I'm hoping this may be a simple answer. It seems like I should be able to post that base 64 encoded string to CakePHP - and that it should update without interpreting/removing any characters from the code. Any advice?

Here is the code snippet of how I'm posting my values to the CakePHP web service:

- (void) updateApi {

NSLog(@"Updating user account settings");
NSString* request_url = @"/users/update";

NSLog(@"%@", [NSString stringWithFormat:@"%@%@", @"http://somesite.com", request_url]);
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://somesite.com", request_url]]];
NSMutableString* post_values = [NSMutableString stringWithFormat:@"data[id]=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]];

[post_values appendFormat:@"&data[company]=%@", [company_field stringValue]];
[post_values appendFormat:@"&data[phone]=%@", [phone_field stringValue]];
[post_values appendFormat:@"&data[address1]=%@", [address1_field stringValue]];
[post_values appendFormat:@"&data[address2]=%@", [address2_field stringValue]];
[post_values appendFormat:@"&data[city]=%@", [city_field stringValue]];
[post_values appendFormat:@"&data[state]=%@", [state_field stringValue]];
[post_values appendFormat:@"&data[zip]=%@", [zip_field stringValue]];
[post_values appendFormat:@"&data[website]=%@", [website_field stringValue]];

// Iteration 09 - Store logo as PNG and prepare to upload to server
NSImage *logoImage = [logo_image image];

if (logoImage) {
    // Image exists; we need to convert it to a BLOB for our back-end database posting
    NSLog(@"Image exists in logo_image");

    // Grab possible representations of this image
    NSArray *representations = [logoImage representations];        
    // Create the PNG data
    NSData *imageData = [NSBitmapImageRep representationOfImageRepsInArray:representations usingType:NSPNGFileType properties:nil];    

    // Base 64 encoding should happen here
    NSString *base64encodedImageData = [imageData base64Encoding];  // Convert to base 64 string
    NSLog(@"\n\nBase 64 image data encoded as \n\n%@\n\n", base64encodedImageData);

    // Store data in parameter
    [post_values appendFormat:@"&data[logo_png_data]=%@", base64encodedImageData];


}

NSString *encoded_post_values = [post_values stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"\n\nEncoded post values string is \n\n%@\n\n", encoded_post_values);

NSData *request_data = [NSData dataWithBytes:[encoded_post_values UTF8String] length:[encoded_post_values length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:request_data];

NSData* return_data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//NSLog(@"%@", [[NSString alloc] initWithData:return_data encoding:NSUTF8StringEncoding]);

NSString* result = [[CJSONDeserializer deserializer] deserialize:return_data error:nil];
//NSLog(@"%@", result);

}

R Brennan
  • 715
  • 1
  • 6
  • 13
  • 1
    What happens here is the + is converted to a space See http://stackoverflow.com/questions/5422028/objective-c-how-to-upload-image-and-text-using-http-post – Martin Samson Jan 23 '12 at 18:11

1 Answers1

0

This is definately an Objective-C Conversion of a space. The cakephp side of it wouldn't touch anything on the base64 encode.

Chris Pierce
  • 706
  • 5
  • 9