Ios 使用Swift在GET请求中添加额外斜杠
我正试图提出GET请求。Url的配置在Url末尾没有斜杠。服务器接收到的url末尾带有“/”,因此我得到错误404。我可以看到带有斜杠的response.url,但urlRequest包含不带斜杠的url 我不明白,发生了什么事 Meant to send: http://someUrl.com/api Sent: http://someUrl.com/api/Ios 使用Swift在GET请求中添加额外斜杠,ios,swift,Ios,Swift,我正试图提出GET请求。Url的配置在Url末尾没有斜杠。服务器接收到的url末尾带有“/”,因此我得到错误404。我可以看到带有斜杠的response.url,但urlRequest包含不带斜杠的url 我不明白,发生了什么事 Meant to send: http://someUrl.com/api Sent: http://someUrl.com/api/ 快速缓存此请求的响应,将CachePolicy设置为忽略重新加载,并尝试将GET请求发送到其他URL,然后重试。您所说的发送是什么意
快速缓存此请求的响应,将
CachePolicy
设置为忽略重新加载,并尝试将GET
请求发送到其他URL,然后重试。您所说的发送是什么意思,您打印的请求是什么?您需要查看服务器配置;似乎/api是一个目录而不是一个“文档”(或端点)@abdelahadarwish我打印的请求是http://someUrl.com/api
,我打印的response.url是http://someUrl.com/api/
这可能来自server@Paulw11服务器?错误是Swift发送请求在末尾添加尾随斜杠。为什么会这样?
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 { ....