gpt4 book ai didi

swift - Alamofire 4 请求出错

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

我必须发出这个获取请求

curl --request GET \
--url http://site.exampleTest \
--header 'authorization: longtoken'

所以我实现了这个功能:

    class func getRequest(endPoint: String, headers: [String : String], success:@escaping(JSON) -> Void,  failure:@escaping(NSError) -> Void) {

Alamofire.request(myBaseURL + endPoint, headers: headers).responseJSON { (responseObject) in
print(responseObject)

if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error = responseObject.result.error!
failure(error as NSError)
}
}

}

然后我这样启动它:

let headerRequest = ["authorization":getUserDefaultElement(key: "token")]

APIManager.getCurrentChatRequest(endPoint: "chats", headers: headerRequest, success: { (JSONObject) in
print(JSONObject.debugDescription)
}, failure: { (error) in
print(error)
})

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) Error Domain=Alamofire.AFError Code=4 "JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format."

同样的道理,我在postman中也没有错误,那我哪里错了呢?

编辑

我得到的应该是这个

    {
"data": [
{
stuff in here
}
]
}

编辑 2

这是从 postman 那里挑选的快速方法,我得到 200 OK:

let headers = [
"authorization": "token" //modified by me
]

let request = NSMutableURLRequest(url: NSURL(string: "http://link.api.me")! as URL, //modified by me
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})

dataTask.resume()

最佳答案

试一试。我相信您没有以正确的格式放置标题。

class func actualReq() {
let headerRequest: HTTPHeaders = [
"authorization":getUserDefaultElement(key: "token")
]

APIManager.getCurrentChatRequest(endPoint: "chats", headers: headerRequest, success: { (JSONObject) in
print(JSONObject.debugDescription)
}, failure: { (error) in
print(error)
})
}

class func getRequest(endPoint: String, headers:HTTPHeaders, success:@escaping(JSON) -> Void, failure:@escaping(NSError) -> Void) {

Alamofire.request(myBaseURL + endPoint, method: .get, headers: headers).responseJSON{ responseObject in
switch response.result {
case .success(let value):
let swiftyJson = JSON(value)
print(value)
success(resJson)
case .failure(let error):
print("Error: \(error)")
failure(error as NSError)

}
}
}

另一种方法:

Alamofire.request(myBaseURL + endPoint, method: .get, headers: headers).responseJSON{ responseObject in
if response.response?.statusCode == 200 {
print ("success")
}else {
guard let responseCode = response.response?.statusCode else {return}
print ("code: \(responseCode)")
}
}
}

关于swift - Alamofire 4 请求出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47397085/

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