iOS app integration with Google Docs text recognition API

I’m working on adding text recognition capabilities to my iOS application using Google Docs API. The documentation mentions that I need to upload files with a specific parameter to enable OCR processing.

According to the docs, I should send a POST request like this:

POST /feeds/default/private/full?ocr=true
GData-Version: 3.0
Authorization: <token here>
Content-Length: 2048
Content-Type: image/jpeg
Slug: Text Recognition Document

... image data here ...

Here’s my current implementation:

mutableResponse = [[NSMutableData data] retain];

NSURL *endpoint = [NSURL URLWithString:@"http://docs.google.com/feeds/default/private/full?ocr=true"];
UIImage *photo = [UIImage imageNamed:@"scan-document.png"];

NSData *imageBytes = UIImagePNGRepresentation(photo);
int size = [imageBytes length];
NSString *contentSize = [NSString stringWithFormat:@"%d", size];

NSMutableURLRequest *httpRequest = [[NSMutableURLRequest alloc] initWithURL:endpoint];
[httpRequest setHTTPBody:imageBytes];

[httpRequest setHTTPMethod:@"POST"];
[httpRequest addValue:@"3.0" forHTTPHeaderField:@"GData-Version:"];
[httpRequest addValue:credentials.token forHTTPHeaderField:@"Authorization:"];
[httpRequest addValue:contentSize forHTTPHeaderField:@"Content-Length:"];
[httpRequest addValue:@"image/png" forHTTPHeaderField:@"Content-Type:"];
[httpRequest addValue:@"Scanned Document" forHTTPHeaderField:@"Slug:"];

[[NSURLConnection alloc] initWithRequest:httpRequest delegate:self];

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

The server keeps returning error 400. What am I doing wrong with this request?

I’ve hit the same issues with Google Docs OCR integration. Your main problem is the header formatting - you’re adding colons after header names, which breaks things. The addValue:forHTTPHeaderField: method wants just the header name, no trailing colon. Drop the colons from “GData-Version:”, “Authorization:”, “Content-Length:”, “Content-Type:”, and “Slug:”. Also, NSURLRequest sets Content-Length automatically, so skip that line. You’re also mixing synchronous and asynchronous connection methods. Pick one - either use delegates with initWithRequest:delegate: or go synchronous with sendSynchronousRequest:, but don’t use both. Lastly, make sure your auth token has the right prefix ("Bearer " or "OAuth ") that Google’s API expects.

check your endpoint url - google killed that old docs api years ago. you need to switch to drive api v3 with vision api for ocr. that old gdata stuff is dead, which is why you’re getting 400 errors.

Had the same headaches migrating legacy OCR code. Beyond the header syntax stuff already mentioned, there’s another gotcha with your auth format. The old GData API wanted “GoogleLogin auth=” prefix for tokens, but since you’re using credentials.token directly, make sure it’s formatted right. Also noticed you’re sending PNG data but your example shows JPEG in the docs - Google’s OCR works better with JPEG anyway for scanned docs. That synchronous connection call will block your UI thread, and Apple flags that in reviews now. I’d switch to NSURLSession for better performance and iOS compatibility. One more thing - test with a smaller image first. Sometimes that 400 error comes from file size limits, not header problems.