2

Previously, I was able to send data from a HTML Form to Google Form (Responses Spreadsheet) using ajax. Here's the code.

Ajax Code

function postContactToGoogle() {
 var email = $('#emailtosubscribe').val();
 $.ajax({
  url: "https://docs.google.com/forms/d/e/[key]/formResponse",
  data: {
   "entry_1064445353": email
  },
  type: "POST",
  dataType: "xml",
  statusCode: {
   0: function() {
    window.location.reload();
   },
   200: function() {
    window.location.reload();
   }
  }
 });
}

Now, I am trying to do the same in Unity using UnityWebRequest. Here's my code

Unity Code

public class SendToGoogle : MonoBehaviour {
    public GameObject email;

    private string Email;

    [SerializeField]
    private string BASE_URL = "https://docs.google.com/forms/d/e/[key]/formResponse";

    IEnumerator Post(string json) {
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(json);

        using (UnityWebRequest www = new UnityWebRequest(BASE_URL, UnityWebRequest.kHttpVerbPOST)) {
            UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
            DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
            www.uploadHandler = uH;
            www.downloadHandler = dH;
            www.SetRequestHeader("Content-Type", "application/json");
            yield return www.Send();

            if (www.isError) {
                Debug.Log(www.error);
            } else {
                Debug.Log(www.ToString());
                Debug.Log(www.downloadHandler.text);
            }
        }
    }

    public void Subscribe() {
        Email = email.GetComponent<InputField>().text;
        var n = new JSONObject();
        n["entry_1064445353"] = Email;
        string json = n.ToString();
        Debug.Log(Email);
        StartCoroutine(Post(json));
    }
}

Now when I try to submit email from the Scene, time stamp is created in the spreadsheet but the email is not saved. I am doubtful on www.SetRequestHeader("Content-Type", "application/json");, as in ajax I am using xml as datatype with dataType: "xml".

Moreover, I also tried sending just string without changing it to JSONObject and changing Content-Type on SetRequestHeader to xml.

Luzan Baral
  • 3,678
  • 5
  • 37
  • 68
  • But your Ajax code is sending with xml and unity is sending is sending with json. Both are not the-same... Are you sure the other end can handle json? – Programmer May 26 '17 at 12:18
  • Other end is Google Form, references I used to create that script 4 month ago I found that datatype should be `xml`, now I want to send data as `xml` from unity. is that possible, could not find good docs on UnityWebRequest SetRequestHeader's content-type. – Luzan Baral May 26 '17 at 12:32
  • Also, confused on the `xml` structure that the google form accepts. – Luzan Baral May 26 '17 at 12:32
  • Haven't done this before. Would have tried if I had access to the key but I don't. – Programmer May 26 '17 at 14:11
  • @Programmer I figured it out using `WWW` and `WWWForm`, i'll add it as an answer. – Luzan Baral May 26 '17 at 14:56
  • what if this is the problem? https://stackoverflow.com/questions/57767790/how-to-add-value-on-another-section-in-google-form-in-unity3d – iamDonMokong Sep 03 '19 at 13:45

1 Answers1

4

Finally, I figured it out using WWW and WWWForm and also managed to create a video tutorial because it was hard for me to get solution for How to save data to Google Spreadsheet from Unity

Video Tutorial : How to save data to Google Spreadsheet from Unity (YouTube)

Here's the code snippet that I had to change.

IEnumerator Post(string email) {
    WWWForm form = new WWWForm();
    form.AddField("entry.1064445353", email);

    byte[] rawData = form.data;
    string url = BASE_URL;

    // Post a request to an URL with our custom headers
    WWW www = new WWW(url, rawData);
    yield return www;
}

public void Subscribe() {
    Email = email.GetComponent<InputField>().text;
    StartCoroutine(Post(Email));
}

If you are having problem finding Google Entry fields check here. How to save data to Google Spreadsheet from Unity 3D using Google Forms

Luzan Baral
  • 3,678
  • 5
  • 37
  • 68