如何将字典作为参数发送到 Get 请求 Swift
How to send Dictionary as parameter to the Get Request Swift
我必须像
一样调用 GET API
需要:
http://ctesterst.net/api/Customers/homeData?lat=12.456&lng=76.786¤t_app_version=123¤t_device=%7B%22Test%22%3A%22123%22%2C%22name%22%3A%22something%22%2C%22tested%22%3Atrue%7D&access_token=Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8
这样我就得打电话了。
输入参数如何添加??
我是这样做的:
我的 json 字符串是:请求模型
struct RequestData: Codable {
struct Devices: Codable {
let test: String
let name: String
let tested:Bool
}
let current_device: Devices
}
我的 url 是:
urlAppend = val + "&lat=\(lati)" + "&lng=\(long)" + "¤t_app_version=\(appVersion)" + "¤t_device=\(jsonString)"
它越来越崩溃了..请给我解决方案。
谢谢
您可以为此使用 URLComponents,URLComponents 是在 Foundation 中定义的。
var components = URLComponents()
components.scheme = "http"
components.host = "ctesterst.net"
components.path = "/api/Customers/homeData"
let queryItemLat = URLQueryItem(name: "lat", value: "12.456")
let queryItemLong = URLQueryItem(name: "lng", value: "76.786")
let queryItemAppVersion = URLQueryItem(name: "current_app_version", value: "123")
let queryItemDevice = URLQueryItem(name: "current_device", value: "something")
let queryItemToken = URLQueryItem(name: "access_token", value: "Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8")
components.queryItems = [queryItemLat, queryItemLong,queryItemAppVersion,queryItemDevice,queryItemToken]
print(components.url)
我必须像
一样调用 GET API需要:
http://ctesterst.net/api/Customers/homeData?lat=12.456&lng=76.786¤t_app_version=123¤t_device=%7B%22Test%22%3A%22123%22%2C%22name%22%3A%22something%22%2C%22tested%22%3Atrue%7D&access_token=Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8
这样我就得打电话了。
输入参数如何添加??
我是这样做的:
我的 json 字符串是:请求模型
struct RequestData: Codable {
struct Devices: Codable {
let test: String
let name: String
let tested:Bool
}
let current_device: Devices
}
我的 url 是:
urlAppend = val + "&lat=\(lati)" + "&lng=\(long)" + "¤t_app_version=\(appVersion)" + "¤t_device=\(jsonString)"
它越来越崩溃了..请给我解决方案。
谢谢
您可以为此使用 URLComponents,URLComponents 是在 Foundation 中定义的。
var components = URLComponents()
components.scheme = "http"
components.host = "ctesterst.net"
components.path = "/api/Customers/homeData"
let queryItemLat = URLQueryItem(name: "lat", value: "12.456")
let queryItemLong = URLQueryItem(name: "lng", value: "76.786")
let queryItemAppVersion = URLQueryItem(name: "current_app_version", value: "123")
let queryItemDevice = URLQueryItem(name: "current_device", value: "something")
let queryItemToken = URLQueryItem(name: "access_token", value: "Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8")
components.queryItems = [queryItemLat, queryItemLong,queryItemAppVersion,queryItemDevice,queryItemToken]
print(components.url)