Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting data to new Google Forms

With the previous(legacy) version of google forms it was possible to programatically post data to the form by sending an HttpPost to a url like this:

https://spreadsheets.google.com/formResponse?formkey=[key]

and data like this:

entry.1.single=data&entry.2.single=moredata

With the new version of google forms (released jan 2013 I think) the url structure is different. Here is a snippet from the "view live form page"

<form action="https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse" method="POST" ...
<input type="text" name="entry.1566150510"

From this code snippet I would think that I could post to a url like this:

https://docs.google.com/forms/d/[key]/formResponse

with data like this:

entry.1566150510=data

But I've tried that from java(android) like this:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    Log.i(myTag, "Inside postData()");
    String fullUrl = "https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse";
    Log.i(myTag, "url = " + fullUrl);
    HttpPost httppost = new HttpPost(urlBase);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("entry.1566150510", "somedata"));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(nameValuePairs);
        Log.i(myTag, data.toString());
        httppost.setEntity(data);
        Log.i(myTag, EntityUtils.toString(data));
        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        Log.i("DocsUploader", "response = " + response);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 

and I get 404, not found response.

Am I missing something obvious here, or has google just removed the ability to post to the new forms?

like image 613
FoamyGuy Avatar asked Nov 16 '25 17:11

FoamyGuy


2 Answers

Consider doing the update with google-apps-script (I suspect it will be less work). And it will continue to work even if google make more tweaks.

like image 182
eddyparkinson Avatar answered Nov 19 '25 07:11

eddyparkinson


Ok, something about my use of HttpClient and HttpPost was done incorrectly. I don't know exactly what I messed up but I am now using this and it is working correctly:

public void postData() {
    String fullUrl = "https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse";
    HttpRequest mReq = new HttpRequest();
    String response = mReq.sendPost(fullUrl, "entry.1566150510=data");
    Log.i(myTag, response);
} 

Where HttpRequest is a helper class created by MattC, and posted as an answer to this question: Secure HTTP Post in Android.

like image 43
FoamyGuy Avatar answered Nov 19 '25 07:11

FoamyGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!