gpt4 book ai didi

json - Alamofire 使用查询参数 status 500

转载 作者:行者123 更新时间:2023-11-30 12:29:26 25 4
gpt4 key购买 nike

我正在调用 Api 路由,以使用经度、纬度和半径获取本地商店的分支机构。 api 路由在 postman 上工作得很好。使用 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)
}
}
}


}

这是我从 postman 那里调用的路线:

http://100.100.70.185:9090/geo/services/3/branches?longitude=31.331358000000002&latitude=30.082763&radius=1000000

当我在 swift 中使用 print() 命令时,这些是我的结果:

print(params) = ["经度": 31.331358000000002, "纬度": 30.082763, "半径": 100000]

打印(路线) =

Optional(http://100.100.70.185:9090/geo/services/3/branches)

关于可选我只是将其展开路线!

关于我在这里写的IP地址不是我使用的真实IP地址,以防万一您测试后没有成功。

最佳答案

实际问题在于编码,您必须根据您的 API 实现使用正确的 ParameterEncoding

Use URLEncoding if the Content-Type HTTP header field of an encoded request with HTTP body is set to application/x-www-form-urlencoded; charset=utf-8

-

Use JSONEncoding if the Content-Type HTTP header field of an encoded request is set to application/json

-

Use PropertyListEncoding if the Content-Type HTTP header field of an encoded request is set to application/x-plist

关于json - Alamofire 使用查询参数 status 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768918/

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