Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:33:23+00:00 2026-06-18T15:33:23+00:00

I currently have a HTTP POST Request and a Base64 Encoding Library, I encode

  • 0

I currently have a HTTP POST Request and a Base64 Encoding Library, I encode my image to B64 then send it over HTTP via the POST method.

I output the Base64 to XCodes console, copy and paste it and it works perfectly. Although the Base64 I store within the Database (MongoDB, Plain Text File etc) always comes out corrupt on the other end.

Working Version (Copied and Pasted from XCode): http://dontpanicrabbit.com/api/working.php
Broken Version (From MongoDB Database): http://dontpanicrabbit.com/api/grabimage.php

If you view the source you’ll notice they are the same but there is added whitespace into the broken version.

The Objective-C code I am using is:

MyImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 0);

    [Base64 initialize];
    NSString *encoded = [Base64 encode:imageData];

    NSString *urlPOST = encoded;
    //NSLog(@"%@",encoded);

    NSString *varyingString1 = @"picture=";
    NSString *varyingString2 = urlPOST;
    NSString *post = [NSString stringWithFormat: @"%@%@", varyingString1, varyingString2];
    NSLog(@"%@", post);
    //NSString *post = @"image=%@",urlPOST;
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"url/api/insertimage.php"]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
    NSString *strResult = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

PHP -> MongoDB Storage

<?php
    try {
      // open connection to MongoDB server
      $conn = new Mongo('localhost');

      // access database
      $db = $conn->dablia;

      // access collection
      $collection = $db->images;

      // insert a new document
      $item = array(
        'picture' => $_POST['picture']
      );
      $collection->insert($item);
      echo 'Inserted document with ID: ' . $item['_id'];

      // disconnect from server
      $conn->close();
    } catch (MongoConnectionException $e) {
      die('Error connecting to MongoDB server');
    } catch (MongoException $e) {
      die('Error: ' . $e->getMessage());
    }
?>

Output Code:

<?php
try {
  // open connection to MongoDB server
  $conn = new Mongo('localhost');

  // access database
  $db = $conn->dablia;

  // access collection
  $collection = $db->images;

  // execute query
  // retrieve all documents
  $cursor = $collection->find();

  // iterate through the result set
  // print each document
  foreach ($cursor as $obj) {
    echo '<img src="data:image/jpeg;base64,'.trim($obj['picture']).'">';
  }

  // disconnect from server
  $conn->close();
} catch (MongoConnectionException $e) {
  die('Error connecting to MongoDB server');
} catch (MongoException $e) {
  die('Error: ' . $e->getMessage());
}
?>

I have no idea why I seem to be corrupting over POST?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:33:24+00:00Added an answer on June 18, 2026 at 3:33 pm

    The problem is exactly what I suggested in my first comment. That is, base64 encoded data can contain ‘+’ characters. In x-www-form-urlencoded data the receiver knows that ‘+’ is an encoding of a space character. Thus since you aren’t URL encoding your base64 value, any instances of ‘+’ will cause the data to be corrupted when received.

    The ‘+’ characters in your initial data are turning into ‘ ‘ when received and stored. When you then output that value, it is invalid base64 encoded data.

    If you examine the source of your working vs. non-working examples you’ll see that the whitespace exists EXACTLY where there is a ‘+’ in the original Base64 encoded value. Any newlines you’re seeing are because whatever you’re viewing the source in is wrapping lines at a ‘ ‘ character.

    In your iOS code you need to properly encode the base64 encoded value, in your case all you really need to do is percent encode the ‘+’ characters.

    EDIT to add, in response to comment:

    post = [post stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently implementing a client application that POST's a file over HTTP and have
I am currently programming an application that needs to send an http Post request
I need to send a http request without encoding (I have to consume a
Currently I know I can send post data to URL through HTTP request. Is
I currently have multiple activity that needs to perform an asynctask for http post
I try to POST QPixmap image bia http. To do that, I have to
I currently am uploading files via HTTP Post, using [body appendData: [NSData dataWithData:data]]; where
I'm attempting to send an HTTP POST to a Rails 3 Application. Currently all
Currently I have: RewriteCond %{HTTP_HOST} ^site\.com [NC] RewriteRule (.*) http://www.site.com/$1 [R=301,L] When I visit
I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.