swift 向Google表单发出POST请求

wpx232ag  于 2023-03-07  发布在  Swift
关注(0)|答案(3)|浏览(165)

我想向Google Form发出POST请求,但我遇到找不到的页面。我正在按照this指南使用如何设置POST请求。
我正在使用这个Google Formhttps://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信息是否正确或者我的实现是否正确。你们怎么看?
下面是我的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"
tjvv9vkg

tjvv9vkg1#

我在一个公用事业项目中完成了这个任务,请在此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)
}

一个常见的错误是使用输入表单的“name”字段,然而您应该使用“id”。

8e2ybdfx

8e2ybdfx2#

斯威夫特4+

Alamofire的帮助下,现在更容易做到这一点
1.查看表单源代码以获取表单URL和每个表单字段的标题。
1.创建插入每个项目的字段ID的数据变量。
1.使用简单的POST请求提交代码。

let data = ["entry.123123123": suggestedActivities, "entry.123124214": suggestedImprovements]

Alamofire.request(googleFormURL, method: .post, parameters: data).validate()
    .responseString {
        response in
        // do your magic
    }
ycl3bljg

ycl3bljg3#

(雨燕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()
    }

为我工作检查:https://docs.google.com/spreadsheets/d/1rYRStyI46L2sjiFF9DTDMlCb2qR2FMtKrZk3USRdXkA/edit#gid=1864592081

相关问题