Http POST to a google form

拜拜、爱过 提交于 2019-12-08 16:45:31

问题


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];

回答1:


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



回答2:


Try to debug and check you final URL it should be something like this https://docs.google.com/forms/d/YOUR_FORM_ID/formResponse?entry.1748727384=test&entry.1949164265=test&submit=Submit




回答3:


I did this task by using Swift. Check it out in this repo: https://github.com/goktugyil/QorumLogs

Here is the tutorial of how to set it up: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

Heres the code to do it:

private static func sendError(#text: String) {
    var url = NSURL(string: formURL)
    var postData = form1Bar + "=" + text
    postData += "&" + form2Bar + "=" + "anothertext"                
    postData += "&" + form3Bar + "=" + "anothertext" 
    postData += "&" + form4Bar + "=" + "anothertext" 

    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
    var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}



回答4:


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


来源:https://stackoverflow.com/questions/18073971/http-post-to-a-google-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!