相关疑难解决方法(0)

如何使用Alamofire 4在Swift 3中为JSON数据发送带有参数和正文的POST请求?

Postman api url在下面添加. 在此输入图像描述

现有代码:

let baseUrl = "abc.com/search/"
let param  =  [
        "page":"1",
        "size":"5",
        "sortBy":"profile_locality"
    ]

    let headers = [
        "Content-Type": "application/json"
    ]


    Alamofire.SessionManager.default.request("\(baseUrl)field", method: .post,parameters: param, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
        print(response.request ?? "no request")  // original URL request
        if(response.response?.statusCode != nil){
            print("done")
            if self.checkResponse(response.response!.statusCode){
                let json = JSON(data: response.data!)
                //print("at_LeadStop json \(json)")
                return completionHandler(json, false)

            } else {
                return completionHandler(JSON.null, true)
            }
        } else {
            print("gone")
            return completionHandler(JSON.null, true)
        }}
Run Code Online (Sandbox Code Playgroud)

我不知道如何通过此代码添加正文请求.请帮我解决这个问题.

json ios alamofire swift3

9
推荐指数
1
解决办法
1万
查看次数

URLQueryItem值是可选的

您好我正在尝试将queryItems添加到我的URLComponents但是因为URLQueryItem返回其值作为可选项,url会在其中保留一个问号.

var params = ["id":"5"]
let queryParams = toStringDictionary(dictionary: params).map { pair in
    return URLQueryItem(name: pair.key, value: pair.value)
}
var components = URLComponents(string: fullURL)
components.queryItems = queryParams
print(components.url)//This is the full url https://demo.com/users/?5
                     //This https://demo.com/users/?5 should be this https://demo.com/users/5
Run Code Online (Sandbox Code Playgroud)

问号当然会导致错误的网址.我无法摆脱它.

ios swift urlsession

2
推荐指数
1
解决办法
244
查看次数

标签 统计

ios ×2

alamofire ×1

json ×1

swift ×1

swift3 ×1

urlsession ×1