swiftpostnsurlrequestnsurlsessiongoogle-forms

Making a POST request to google form


I would like to make a POST request to a Google Form but I'm coming across page not found. I am following this guide on using on how to set up the POST request.

I am using this Google Form: https://docs.google.com/forms/d/1knZJE-3afDGgW9Sqg6Cf6WiMdCSDAkgvi-wc6aRXV3k/viewform

The form has 3 fields and a few hidden input fields such as draftResponse and pagehistory which the tutorial does not mention.

let url = NSURL(string: "https://docs.google.com/spreadsheets/d/1YrQ71fhsX4XQtwV8qHpoHreBeyl1EnNo2pvafsoygtk/edit#gid=990368688")

var request = NSMutableURLRequest(URL: url!)
let session = NSURLSession.sharedSession()

request.HTTPMethod = "POST"

var fieldsToPost = "entry.1959479545=test1&entry.354177126=test2&entry.1807599304=test@gmail.com" as NSString

request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")

request.HTTPBody = fieldsToPost.dataUsingEncoding(NSUTF8StringEncoding)

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

    println("Response \(response)")

    var strData = NSString(data: data, encoding: NSUTF8StringEncoding)

    println(strData)

    if error == nil {
      println("error")
    }

  })


task.resume()

Not sure if my POST information is correct or my implementation. What do you guys think?

Bellow are my Google Form page source input fields:

input type="text" name="entry.1959479545" value="" class="ss-q-short" id="entry_1959479545" dir="auto" aria-label="First Name  " title=""
input type="text" name="entry.354177126" value="" class="ss-q-short" id="entry_354177126" dir="auto" aria-label="Last Name  " title=""
input type="hidden" name="draftResponse" value="[,,"-7091411838997769820"]"
input type="hidden" name="pageHistory" value="0"
input type="hidden" name="fbzx" value="-7091411838997769820"

Solution

  • I did this task in a utility project I was working on, 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 = formField1 + "=" + text
        postData += "&" + formField2 + "=" + "anothertext"                
        postData += "&" + formField3 + "=" + "anothertext" 
        postData += "&" + formField4 + "=" + "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)
    }
    

    A common mistake is using the "name" field of the input form, however you should use the "id".