向谷歌表单进行HTTP POST请求

11

我需要对我的Google表单进行Http POST请求,并且根据建议(http://productforums.google.com/forum/#!topic/docs/iCfNwOliYKY)创建了以下URL语法:

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

最终我将要在iOS和Android上执行POST,但我现在仅在浏览器中测试它并且它无法工作。我收到了一个Google Drive响应:"抱歉,您请求的文件不存在。" 并且该条目未进入我的响应电子表格。

我错过了什么?我已经到处找过了,但没有找到答案。

编辑:

我想有时直接在目标平台上进行测试可能更好。以下代码适用于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];
4个回答

6

向以下URL发送POST请求:

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

在预览表单时,你可以在URL中找到 FORM_ID。

要获取字段ID,请检查每个输入标签的“name”属性。或者在表单页面上运行以下脚本。

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

应该在您的控制台中打印出类似于这个的东西。
Field1: entry.748645480
Field2: entry.919588971

您可以获得一个预填链接,用于输入ID。 - undefined

1
如果您使用Google Chrome开发者工具或任何浏览器的工具查看表单,您会看到每个控件都有一个名称。例如:name='entry.123456789' 在C#中,您可以像这样设置表单上的答案:
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);
}

您还可以在URL中设置答案:

https://docs.google.com/forms/d/[form id]/viewform?entry.123456789=Answer1&entry.987654321=Answer2


1
我使用Swift完成了这个任务。在这个存储库中查看它:https://github.com/goktugyil/QorumLogs 以下是设置教程:https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md 以下是相关代码:
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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接