简体   繁体   English

如何将字典作为参数发送到获取请求 Swift

[英]How to send Dictionary as parameter to the Get Request Swift

i have to call GET API like我必须像这样调用 GET API

required:必需的:

http://ctesterst.net/api/Customers/homeData?lat=12.456&lng=76.786&current_app_version=123&current_device=%7B%22Test%22%3A%22123%22%2C%22name%22%3A%22something%22%2C%22tested%22%3Atrue%7D&access_token=Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8

like that i have to call.像那样我必须打电话。

input parameters how to add??输入参数怎么加??

i am doing like this:我正在这样做:

my json string is: request model我的 json 字符串是:请求 model

 struct RequestData: Codable {
                struct Devices: Codable {
                    let test: String
                    let name: String
                    let tested:Bool
                }
                let   current_device: Devices

            }

and my url is:我的 url 是:

urlAppend = val + "&lat=\(lati)" + "&lng=\(long)" + "&current_app_version=\(appVersion)" + "&current_device=\(jsonString)"

its getting crash.. please give me solution.它崩溃了..请给我解决方案。

Thanks谢谢

You can use URLComponents for this, URLComponents is defined in Foundation.您可以为此使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM