gpt4 book ai didi

ios - 在swift中使用json发送参数

转载 作者:行者123 更新时间:2023-11-28 12:08:46 24 4
gpt4 key购买 nike

我正在尝试在我的代码中使用 webapi。我的 JSON 服务有 odata 过滤。但是,当我尝试将我的链接发送到我的 JSON 序列化程序时,我在 myURL2 收到“nil”响应。但是,如果我不带参数发送它就可以正常工作。你有什么建议吗?如何在我的代码中使用 ODATA?

        let link = "https://apiservices.domain.com/api/Events?$filter=EventDate gt datetime'2018-02-01' and EventDate lt datetime'2018-02-15'"

let myURL2 = NSURL(string: link)
let request2 = NSMutableURLRequest(url: myURL2 as! URL)
request2.httpMethod = "GET"


request2.addValue("Bearer "+tokenNewId, forHTTPHeaderField: "Authorization")
request2.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request2.setValue("application/json", forHTTPHeaderField: "Accept")

let task2 = URLSession.shared.dataTask(with: request2 as URLRequest) {(data2, response2, error2) -> Void in
if let unwrappedData2 = data2 {
do {
guard let records = try? JSONSerialization.jsonObject(with: unwrappedData2, options: .mutableContainers) as? [[String: Any]] else {
return
}
}

最佳答案

试试这个:

let urlwithPercentEscapes = link.addingPercentEncoding( 
withAllowedCharacters: .urlQueryAllowed)
let myURL2 = NSURL(string: urlwithPercentEscapes!)

关于ios - 在swift中使用json发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com