我试图将一个JSON主体发送到REST API,但它只读取第一个参数,根本无法识别JSON的其余部分。我测试了JSON主体的其他部分,并没有像通常那样得到错误。
func postRequest(classroomID: String, email: String, vote: String){
//declare parameter as a dictionary which contains string as key and value combination.
let parameters = [
"classroomID": classroomID,
"LastUpdated": "2020-01-01",
"TheVoteData"[
"Email": email,
"TheVote": vote
]
]
//create the url with NSURL
let url = URL(string: "https://www.api-gateway/dynamoDB/resource")!
//create the session object
let session = URLSession.shared
//now create the Request object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to data object and set it as request body
} catch let error {
print(error.localizedDescription)
}
//HTTP Headers
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//create dataTask using the session object to send data to the server
let task = session.dataTask(with: request, completionHandler: { data, response, error in
guard error == nil else {
completion(nil, error)
return
}
guard let data = data else {
return
}
do {
//create json object from data
guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
return
}
print(json)
completion(json, nil)
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}
这将张贴classroomID到数据库,但不是电子邮件或投票。我从这里得到了这个方法:如何在Swift中用JSON体制作HTTP Post请求
我不认为您在参数中提供的JSON数据是有效的(我使用jsonlint.com进行检查)
func postRequest(classroomID: String, email: String, vote: String){
//declare parameter as a dictionary which contains string as key and value combination.
let parameters: [String:Any] = [
"classroomID": classroomID,
"LastUpdated": "2020-01-01",
"TheVoteData":[
"Email": email,
"TheVote": vote
]
]
//create the url with NSURL
let url = URL(string: "https://www.api-gateway/dynamoDB/resource")!
//now create the Request object using the url object
var request = URLRequest(url: url)
request.httpMethod = "POST" //set http method as POST
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to data object and set it as request body
} catch let error {
print(error.localizedDescription)
}
//HTTP Headers
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
//create dataTask using the session object to send data to the server
let task = URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print(dataString)
}
//Returns HHTP response if
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
}
}
task.resume()
}
我对斯威夫特还是个新手。我正在尝试制作一个HTTPS POST到HTTPS url的特定头。注意HTTPS而不是HTTP。我如何完成这件事?提前道谢。
我正在尝试从HTML中读取post请求参数。我可以使用以下JavaScript代码读取get请求参数。 但对post请求不起作用。有人能告诉我如何使用JavaScript读取HTML中的post请求参数值吗?
本文向大家介绍如何使用Swift在iOS App上发出HTTP POST请求?,包括了如何使用Swift在iOS App上发出HTTP POST请求?的使用技巧和注意事项,需要的朋友参考一下 要在iOS中发出http请求,我们将使用DataTask和会话。我们将创建配置,会话,URL,请求和dataTask对象。让我们看看我们将要执行的步骤。 首先我们需要创建一个会话对象,这是默认配置。 然后,我
问题内容: 我在iBooks中阅读了Apple的The Programming Language Swift ,但无法弄清楚如何在Swift中发出HTTP请求(例如cURL)。我需要导入Obj- C类还是只需要导入默认库?还是无法基于本机Swift代码发出HTTP请求? 问题答案: 您可以使用,而且还是因为你通常在Objective- C做。请注意,对于iOS 7.0和更高版本,它是首选。 使用
我在iBooks上读过Apple的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似于cURL)。我需要导入Obj-C类还是只需要导入默认库?或者不可能基于本机Swift代码发出HTTP请求?
我在iBooks中阅读了苹果公司的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似cURL的东西)。我需要导入Obj-C类还是只需要导入默认库?还是不能基于原生Swift代码进行HTTP请求?