我有一个post方法,其中包含基本url和附加在基本url本身中的各种参数。 代码如下:
let todosEndpoint:String = "https://xxxxxxx/api/post_schedule_form_data?service_type=nanny&start_date=07/20/2020&start_time=06:00&end_date=07/20/2020&end_time=09:00&work_description=Work Description&special_instructions=Special Instructions&location_address=location_address&postal_code=abc123¤t_profile_id=10"
let header: HTTPHeaders = ["Content-Type":"application/json","x-token":self.usertoken!]
print("the url is",todosEndpoint)
AF.request(todosEndpoint, method: .post, encoding: JSONEncoding.default, headers: header)
.responseJSON { response in
switch response.result {
case .success(let json):
print("Validation Successful",json)
case let .failure(error):
print(error)
}
}
我在代码中没有得到任何回应。错误可能是什么?
答案 0 :(得分:0)
在我看来,您在查询参数中有很多非法字符(例如“:”,“ /”,““)。
我建议您使用URLComponents构建URL。传递端点和查询参数,然后返回路径。那会告诉你什么需要逃脱。
我猜我对“:”和“ /”错误。看起来这些是合法的,作为查询参数值的一部分。
此代码:
var components = URLComponents()
components.scheme = "https"
components.host = "xxxxxxx"
components.path = "/api/post_schedule_form_data"
components.queryItems = [
URLQueryItem(name: "service_type", value: "nanny"),
URLQueryItem(name: "start_date", value: "07/20/2020"),
URLQueryItem(name: "start_time", value: "06:00"),
URLQueryItem(name: "end_date", value: "07/20/2020"),
URLQueryItem(name: "end_time", value: "09:00"),
URLQueryItem(name: "work_description", value: "Work Description"),
URLQueryItem(name: "special_instructions", value: "Special Instructions"),
URLQueryItem(name: "location_address", value: "location_address"),
URLQueryItem(name: "postal_code", value: "abc123"),
URLQueryItem(name: "current_profile_id", value: "10"),
]
print(components.string ?? "nil")
输出字符串
https://xxxxxxx/api/post_schedule_form_data?service_type=nanny&start_date=07/20/2020&start_time=06:00&end_date=07/20/2020&end_time=09:00&work_description=Work%20Description&special_instructions=Special%20Instructions&location_address=location_address&postal_code=abc123¤t_profile_id=10
请注意,空格以%20
的形式转义。
这可能是您的URL字符串中唯一非法的部分。
URLComponents
是您的朋友,因为它根据需要将转义应用于URL的不同部分,并对URL的每个不同部分实施正确的转义规则。
此代码:
let urlString: String = "https://xxxxxxx/api/post_schedule_form_data?service_type=nanny&start_date=07/20/2020&start_time=06:00&end_date=07/20/2020&end_time=09:00&work_description=Work Description&special_instructions=Special Instructions&location_address=location_address&postal_code=abc123¤t_profile_id=10"
let componentsFromString = URLComponents(string: urlString)
print(componentsFromString?.string ?? "nil" )
显示“无”
这表明您的URL字符串有违法之处。
答案 1 :(得分:0)
您的标题没有正确通过... 另外,您正在URL中传递“”(空格),只需将其替换为“%20”即可。 正确的方法是:
let url:String = "https://xxxxxxx/api/post_schedule_form_data?service_type=nanny&start_date=07/20/2020&start_time=06:00&end_date=07/20/2020&end_time=09:00&work_description=Work Description&special_instructions=Special Instructions&location_address=location_address&postal_code=abc123¤t_profile_id=10".replacingOccurrences(of: " ", with: "%20")
var request = URLRequest(url: NSURL(string: url)! as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(self.usertoken!, forHTTPHeaderField: "x-token")
AF.request(request).responseJSON { response in
switch response.result {
case .success(let json):
print("Validation Successful",json)
case let .failure(error):
print(error)
}
}
您正在强行解压self.usertoken,因此也要注意! 似乎URL中的日期带有“ /”,您必须将其更改为URL中的“%2F”,或者更好地将其设置为后端格式!