Post from Java to a Google docs form

CodeDevTutorials

A working example : sending a request to a Google doc form with okhttp.

Create a form:
35048154_1

Add a question:
35048154_2

See the form once done:
35048154_3

You have to find the name of your inputs by looking at the source code of the form, the name starts with entry. following by a number:

<input name="entry.361168075" value="" class="ss-q-short" id="entry_361168075" dir="auto" aria-label="What's your favorite color  " aria-required="true" required="" title="" type="text">

Write a small program:

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Test {

  public static void main( String[] args ) {
    try {
      OkHttpClient client = new OkHttpClient();
      FormBody body = new FormBody.Builder()
          .add( "entry.361168075", "Red" )
          .build();
      Request request = new Request.Builder()
          .url( "https://docs.google.com/forms/d/1k96sccTC_24b6jo9Fs4h_ML-FVKHCMElgjUzOwSkwu4/formResponse" )
          .post( body )
          .build();
      Response response = client.newCall( request ).execute();
      System.out.println( response.isSuccessful() );

    }
    catch ( Exception e ) {
      e.printStackTrace();
    }
  }
}

And see the result in the spreadsheet output:
35048154_4

🙂

Previous
Android, NodeJS, Openshift, Twilio
Next
3 minutes SVN server

Leave a comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.