更新时间:2023-01-23 19:30:22
你在这里混合两件事, page
, size
和 sortBy
您需要将url字符串作为查询字符串传递。现在您的身体请求是 JSON
数组,您只能使用 URLRequest发布带有
。所以试试这样。 Alamofire
的数组
You are mixing two things here, page
,size
and sortBy
is you need to pass with the url string as query string. Now your body is request is JSON
Array and you can post array with Alamofire
only using URLRequest
. So try like this.
let baseUrl = "abc.com/search/"
let queryStringParam = [
"page":"1",
"size":"5",
"sortBy":"profile_locality"
]
//Make first url from this queryStringParam using URLComponents
var urlComponent = URLComponents(string: baseUrl)!
let queryItems = queryStringParam.map { URLQueryItem(name: $0.key, value: $0.value) }
urlComponent.queryItems = queryItems
//Now make `URLRequest` and set body and headers with it
let param = [
[
"fieldName" : "abc",
"fieldValue":"xyz"
],
[
"fieldName" : "123",
"fieldValue":"789"
]
]
let headers = [ "Content-Type": "application/json" ]
var request = URLRequest(url: urlComponent.url!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: param)
request.allHTTPHeaderFields = headers
//Now use this URLRequest with Alamofire to make request
Alamofire.request(request).responseJSON { response in
//Your code
}