gpt4 book ai didi

ios - 为什么我可能会在此 http POST 请求上收到内部服务器错误 (500)

转载 作者:行者123 更新时间:2023-11-28 08:17:09 25 4
gpt4 key购买 nike

多年来,我一直在与这个问题作斗争。我正在尝试将图像上传到服务器,但一直收到 500 错误。

这是我用来获取图像的代码,对其进行 base64 编码,然后将其添加到字典中。

 if let imageDataToUpload = UIImageJPEGRepresentation(selectedImage, 1.0) {


let encodedImageData = imageDataToUpload.base64EncodedString(options: [])

let extras = "data:image/jpeg;base64,"

let postBody = [
"image": extras + encodedImageData,
"id": id,
"instruction": "1",
"ext": ""
]

let endPoint = UrlFor.uploadPhoto

ApiManager().apiPostImage(endPoint: endPoint, postBody: postBody, callBackFunc: handleResultOfUploadPhoto) }

下面是我用来执行实际 POST 请求的代码。您会注意到我正在尝试将帖子正文转换为 JSON 对象。

func apiPostImage(endPoint: String, postBody: [String: Any]?, callBackFunc: @escaping (ResultOfApiCall) -> Void) {

// get the sessionKey
guard let sessionKey = KeyChainManager().getSessionKey() else {
print("error getting session key")
AppDelegate().signUserOut()
return
}

// create a url with above end point and check its a valid URL
guard let url = URL(string: endPoint) else {
print("Error: cannot create URL")
return
}

// set up the request
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"

if let postBody = postBody {
do {
try urlRequest.httpBody = JSONSerialization.data(withJSONObject: postBody, options: [])
} catch {
print("problems creating json body")
}
}

// set up the header
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = [
"Accept": "application/json",
"apiKey": "0101010-0101010", // not the real apiKey
"usrKey": sessionKey,
"appInfo" : "appcode:1000|version:2.0.37",
"Content-Type": "application/json"
]

let session = URLSession(configuration: config)

let task = session.dataTask(with: urlRequest, completionHandler:
{ (data: Data?, response: URLResponse?, error: Error?) -> Void in

let (noProblem, ResultOfCall) = self.checkIfProblemsWith(data: data, response: response, error: error)

guard noProblem else {
callBackFunc(ResultOfCall)
return
}

let serializedData: [String:Any]
do {

// safe bc checked for nil already.
serializedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]

} catch {

callBackFunc(ResultOfApiCall.errorWhileSerializingJSON)
return
}

// pass serialized data back using the callBackFunc
callBackFunc(ResultOfApiCall.success(serializedData))
})

task.resume()
}

API 是 RESTful 的。我无权访问 API 错误日志,但我从 API 返回此错误:

["codesc": GENERALERROR, "code": 5001, "msg": Conversion from string "Optional(1004007)" to type 'Double' is not valid.]

最佳答案

500 Internal Server Error 是服务器端错误,这意味着问题可能与您的计算机或互联网连接无关,而是网站服务器的问题。

关于ios - 为什么我可能会在此 http POST 请求上收到内部服务器错误 (500),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270578/

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