Swift 5 NSURLComponents 查询字符串 %3F
Swift 5 NSURLComponents Query String %3F
我正在从远程服务器获取 json。
当我构建 url 时,查询字符串是一个 ?,但是,当它通过数据任务发送时, ?被编码为 %3F,我的调用失败了。
如何防止 url组件对查询字符串进行编码?我已经换掉了实际值。
//Note: the host and path are coming from info.plist, I just added them in here.
string host = servicehost.com
string path = "/service/getData?cc=al&lastDate="
let urlComponents = NSURLComponents.init()
urlComponents.scheme = "https"
urlComponents.host = host
urlComponents.path = path
当我打印出值:时,我得到
print(": urlComponents.url! \(urlComponents.url!)")
https://servicehost.com/service/getData%3Fcc=al&lastDate=
在 dataTask 内部,检查时出现以下错误:
Printing description of error:
▿ DecodingError
▿ typeMismatch : 2 elements
- .0 : Swift.String
▿ .1 : Context
▿ codingPath : 3 elements
- 0 : CodingKeys(stringValue: "data", intValue: nil)
- 1 : CodingKeys(stringValue: "date-published", intValue: nil)
- 2 : CodingKeys(stringValue: "date", intValue: nil)
- debugDescription : "Expected to decode String but found a number instead."
- underlyingError : nil
Printing description of data:
▿ 0 bytes
- count : 0
▿ pointer : 0x00007f90d2fdd000
- pointerValue : 140259991867392
- bytes : 0 elements
数据大小为:
jsonDecodableTask: 68 bytes
失败信息是:
Failure: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
如果我转到浏览器并输入带有编码查询字符串的 url,我将返回以下内容:
<html><head><title>Error</title></head><body>Not Found</body></html>
如果我转到浏览器并将 %3F 更改为 ?,则请求成功。
问题是 "?"
不是路径的一部分。它应该在您添加 URL:
的查询项组件时自动添加
let host = "www.google.com"
let path = "/service/getData"
let queryItems: [URLQueryItem] = [.init(name: "cc", value: "al"),
.init(name: "lastDate", value: "")]
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = queryItems
urlComponents.url?.absoluteString // "https://www.google.com/service/getData?cc=al&lastDate="
我正在从远程服务器获取 json。
当我构建 url 时,查询字符串是一个 ?,但是,当它通过数据任务发送时, ?被编码为 %3F,我的调用失败了。
如何防止 url组件对查询字符串进行编码?我已经换掉了实际值。
//Note: the host and path are coming from info.plist, I just added them in here.
string host = servicehost.com
string path = "/service/getData?cc=al&lastDate="
let urlComponents = NSURLComponents.init()
urlComponents.scheme = "https"
urlComponents.host = host
urlComponents.path = path
当我打印出值:时,我得到
print(": urlComponents.url! \(urlComponents.url!)")
https://servicehost.com/service/getData%3Fcc=al&lastDate=
在 dataTask 内部,检查时出现以下错误:
Printing description of error:
▿ DecodingError
▿ typeMismatch : 2 elements
- .0 : Swift.String
▿ .1 : Context
▿ codingPath : 3 elements
- 0 : CodingKeys(stringValue: "data", intValue: nil)
- 1 : CodingKeys(stringValue: "date-published", intValue: nil)
- 2 : CodingKeys(stringValue: "date", intValue: nil)
- debugDescription : "Expected to decode String but found a number instead."
- underlyingError : nil
Printing description of data:
▿ 0 bytes
- count : 0
▿ pointer : 0x00007f90d2fdd000
- pointerValue : 140259991867392
- bytes : 0 elements
数据大小为:
jsonDecodableTask: 68 bytes
失败信息是:
Failure: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
如果我转到浏览器并输入带有编码查询字符串的 url,我将返回以下内容:
<html><head><title>Error</title></head><body>Not Found</body></html>
如果我转到浏览器并将 %3F 更改为 ?,则请求成功。
问题是 "?"
不是路径的一部分。它应该在您添加 URL:
let host = "www.google.com"
let path = "/service/getData"
let queryItems: [URLQueryItem] = [.init(name: "cc", value: "al"),
.init(name: "lastDate", value: "")]
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = host
urlComponents.path = path
urlComponents.queryItems = queryItems
urlComponents.url?.absoluteString // "https://www.google.com/service/getData?cc=al&lastDate="