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?