Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http POST to a google form

I need to do an Http POST to my google form, and following an advice (http://productforums.google.com/forum/#!topic/docs/iCfNwOliYKY) I created a URL syntax like this:

https://docs.google.com/forms/d/MY_FORM_ID&entry.2087820476=hello&entry.261928712=dear&submit=Submit

Eventually I'll do the POST from iOS and Android, but I'm testing it simply in the browser and it doesn't work. I get back a Google Drive response that says "Sorry, the file you have requested does not exist." and the entry doesn't go into my responses spreadsheet.

What am I missing? I looked everywhere but nothing seem to answer this question.

EDIT:

I guess sometimes it's better to test directly on your target platform. The following code works for iOS:

NSString *post = @"&key1=val1&key2=val2";
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/MY_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];
like image 242
Eddy Avatar asked Aug 06 '13 07:08

Eddy


2 Answers

If you look at the form using Google Chrome developer tools, or the tools of any browser, you will see that each control has a name. Example: name='entry.123456789'

In C# you can set the answers on a form like this:

private static void PostToForm()
{
    WebClient client = new WebClient();

    var keyValue = new NameValueCollection();
    keyValue.Add("entry.123456789", "My answer #1");
    keyValue.Add("entry.987654321", "My answer #2");

    Uri uri = new Uri("https://docs.google.com/forms/d/[form id]/viewform");

    byte[] response = client.UploadValues(uri, "POST", keyValue);
}

You can also set the answers in the URL:

https://docs.google.com/forms/d/[form id]/viewform?entry.123456789=Answer1&entry.987654321=Answer2
like image 130
Karl Gjertsen Avatar answered Sep 26 '22 13:09

Karl Gjertsen


Send a POST to following url

https://docs.google.com/forms/d/e/[FORM_ID]/formResponse?entry.1098499744=AnswerField1&entry.1090138332=AnswerField2

You can find your FORM_ID in the url when you preview your form.

To get the field ids, check the "name" attribute of each input tag. Or run the following script in the console on the form page.

function loop(e){
if(e.children)
    for(let i=0;i<e.children.length;i++){
        let c = e.children[i], n = c.getAttribute('name');
        if(n) console.log(`${c.getAttribute('aria-label')}: ${n}`);
        loop(e.children[i]);
     }
}; loop(document.body);

Should print something similar to this in your console

Field1: entry.748645480
Field2: entry.919588971
like image 27
brunettdan Avatar answered Sep 23 '22 13:09

brunettdan