Reputation: 95
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&[email protected]" 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"
Upvotes: 4
Views: 3556
Reputation: 1
( Swift 5 )
private func postRequest2(name: String, email: String, phoneNumber: String, note: String){
var url = "https://docs.google.com/forms/d/19MbGnGA54cj9nobK5FxvRNcXJ-Gtudb_xSA3VChzSxU/formResponse?entry.631576183=\(name)&entry.922538006=\(email)&entry.836974774=\(phoneNumber)&entry.526236259=\(note)"
var request = URLRequest(url: URL(string: url)!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
return
}
print(String(data: data, encoding: .utf8)!)
}
task.resume()
}
working for me checked: https://docs.google.com/spreadsheets/d/1rYRStyI46L2sjiFF9DTDMlCb2qR2FMtKrZk3USRdXkA/edit#gid=1864592081
Upvotes: 0
Reputation: 793
Swift 4+
This is easier to do now with the help of Alamofire
let data = ["entry.123123123": suggestedActivities, "entry.123124214": suggestedImprovements]
Alamofire.request(googleFormURL, method: .post, parameters: data).validate()
.responseString {
response in
// do your magic
}
Upvotes: 0
Reputation: 39181
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".
Upvotes: 3