【问题标题】:How to send Dictionary as parameter to the Get Request Swift如何将字典作为参数发送到 Get Request Swift
【发布时间】:2020-08-11 05:10:07
【问题描述】:

我必须像

一样调用 GET API

必填:

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

这样我必须打电话。

输入参数如何添加??

我正在这样做:

我的 json 字符串是:请求模型

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

            }

我的网址是:

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

它正在崩溃..请给我解决方案。

谢谢

【问题讨论】:

    标签: ios json swift iphone swift4


    【解决方案1】:

    您可以为此使用 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)
    

    【讨论】:

    • @Dara tx 用于重播 .. 但即使我用户也回答了 .. 它给出了相同的输出。
    • 它崩溃了吗?您是否尝试调用在 components.url 中准备的 url ?
    • 不会崩溃...网址很好。我的输入问题 - 编码问题。我把你的答案作为参考......它的工作。
    • 你的回答解决了我 80% 的输入问题。非常感谢。
    • 很乐意为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多