我正在使用经度,纬度和频率调用Api路线来获取本地商店的分支机构。半径。 api路线在邮递员上工作得很好。使用Alamofire它正在回复内部服务器错误500.我怀疑问题是在参数中,但我只测试了所有内容和每个组合都没有成功。
这是我的HTTP请求:
let branchesRoute = "geo/services/"
//Get branches within location radius
func getBranchesWithRadius (serviceID: Int, location: CLLocation, completion: @escaping (_ status:Bool, _ error:Error?, _ msg:String?, _ branches:NSArray?) ->())
{
let route = URL(string:branchIp + branchesRoute + String(serviceID) + "/branches")
print(route)
let header: HTTPHeaders = [
"Content-Type":"application/json",
"auth-token": Helper.shared.getApiKey()//"\(Helper.shared.getApiKey())"
]
let params: Parameters = [
"longitude" : location.coordinate.longitude,
"latitude" : location.coordinate.latitude,
"radius" : Helper.shared.getRadius()
]
print(params)
Alamofire.request(route!, method: .get, parameters: params, encoding: JSONEncoding.default, headers: header)
.validate(statusCode: 200..<300)
.responseJSON { (response) in
switch response.result {
case .success( _):
let json = response.result.value!
// let swiftyJson = JSON(json)
completion(true, nil, nil, json as? NSArray)
// print(json)
case .failure(let err):
print(err)
if response.response?.statusCode == 401 {
completion(false,err,"Unauthorized",nil)
} else {
completion(false, err, "Error getting branches",nil)
}
}
}
}
这是来自邮递员的路线:
当我在swift中使用print()
命令时,这些是我的结果:
print(params)
= ["longitude": 31.331358000000002, "latitude": 30.082763, "radius": 100000]
print(route)
=
Optional(http://100.100.70.185:9090/geo/services/3/branches)
关于Optional
我只是打开它route!
关于我在这里写的ip地址并不是我在测试时使用的真实地址,但它没有用完。
答案 0 :(得分:0)
实际问题出在编码中,您必须根据API实现使用正确的ParameterEncoding
。
如果带有HTTP正文的已编码请求的
,请使用URLEncoding
HTTP标头字段设置为Content-Type
application/x-www-form-urlencoded; charset=utf-8
-
如果
JSONEncoding
的HTTP标头字段为 编码的请求设置为Content-Type
-
如果
application/json
的HTTP标头字段为{ 编码的请求设置为PropertyListEncoding