I need to do a HTTP Post from my iPhone app to a Google Doc. It works fine for English, but the Hebrew shows as question marks in the Google Doc.
This is what I'm doing:
NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
What am I missing?
EDIT: I tried the solution offered here and looked at ahmedalkaf's link here but no luck.
You have set your Content-Type to application/x-www-form-urlencoded
, but you haven't url-encoded your contents.
URL-encoding your post NSString and changing the Encoding to UTF-8 (I cant tell you why, but it's needed to make it work) should do the job.
NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil];
post =[post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
Everything else can stay the same. However you should think about using an asynchronous request. When using a synchronous request, your user interface will become unresponsive, until the request is finished. This doesn't happen with an asynchronous request.
For HTTP-Requests I usually use the ASIHTTPRequest Library: http://allseeing-i.com/ASIHTTPRequest/
It takes a couple of minutes to integrate into your project, but it makes everything way more simple. You don't have to mess around with encodings etc.
NSURL *url =[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:self.firstName.text forKey:@"entry.xxxx"];
[request addPostValue:self.phone.text forKey:@"entry.yyyy"];
[request addPostValue:self.email.text forKey:@"entry.zzzz"];
[request startAsynchronous];
That's it.
To set up ASIHTTPRequest follow these instructions http://allseeing-i.com/ASIHTTPRequest/Setup-instructions and remember to add a -fno-objc-arc
compiler flag to the libraries files under Project Settings -> Build Phases -> Compile Sources if you are using ARC.