我正在尝试发出 GET 请求。 URL 配置为在 URL 的末尾不带斜线。服务器在最后收到带有“/”的 url,所以我收到错误 404。我可以看到带有斜杠的 response.url,但 urlRequest 包含没有它的 URL。
不明白,这是怎么回事。
Meant to send: http://someUrl.com/api Sent: http://someUrl.com/api/
Code sample
guard let url = URL(string: self.rootUrl + "/api") else {
print ("Can't make URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
let sessionConf = URLSessionConfiguration.default
let session = URLSession.init(configuration: sessionConf)
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /api")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
print (urlRequest)
let json = try? JSONSerialization.jsonObject(with: responseData) as? [String: Any]
if ( json == nil ) {
print ("error json")
print(response!)
} else { ....
对此请求的 Swift 缓存响应,设置 CachePolicy
以忽略重新加载并尝试向其他 URL 发出 GET
请求,然后再试一次。
关于ios - 使用 Swift 的 GET 请求中的额外斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709895/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |