我想向Google Form
发出POST请求,但我找不到页面。我正在关注使用如何设置POST请求的this指南。
我正在使用此Google Form
:https://docs.google.com/forms/d/1knZJE-3afDGgW9Sqg6Cf6WiMdCSDAkgvi-wc6aRXV3k/viewform
表单有3个字段和一些隐藏的输入字段,例如draftResponse和pagehistory,教程没有提及。
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()
不确定我的POST信息是否正确或我的实施。你们觉得怎么样?
Bellow是我的Google Form
页面来源输入字段:
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"
答案 0 :(得分:2)
我在我正在处理的实用程序项目中执行了此任务,请在此repo中查看:https://github.com/goktugyil/QorumLogs
以下是如何设置它的教程: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md
继承代码:
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)
}
常见的错误是使用"名称"输入表单的字段,但您应该使用" id"。
答案 1 :(得分:0)
Swift 4
在[Alamofire] [1]
的帮助下,现在更容易做到使用简单的.post请求提交代码
让数据= [" entry.123123123" :suggestedActivities," entry.123124214" :recommendedImprovements]
Alamofire.request(googleFormURL,方法:.post,参数:数据).validate() .responseString {response
在 }
[1]:https://git
hub.com/Alamofire/Alamofire