1

我一直在尝试使用 CloudSight API 使用 Swift 4 和 iOS 11 发送图像。我目前正在将图像转换为 base64 编码字符串并将数据作为 JSON 发送到 CloudSight 服务器,但我总是得到一个空响应。CloudSight 工作人员建议我不要使用 base64,我应该使用 multipart/form-data 请求。任何帮助都将不胜感激,因为 CloudSight 文档并未完全涵盖如何使用存储在变量中的图像,例如我的案例,因此我不确定在创建 POST 参数时应该使用的键,以及如何构建多部分在这种情况下请求。imageData 变量正在转换一个 UIImage,它是 iPhone 7 拍摄的照片,所以我猜测图像大小可能是个问题。我很乐意回答任何其他问题,再次感谢您的帮助!我已经安装了 CloudSight Pod 并导入了它,不确定这是否会有所作为。

这是我当前遇到问题的代码(我用我的实际密钥替换了 API KEY):

        let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
        let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
        let rawimage = UIImageJPEGRepresentation(image, 0.7)!




let url2 = URL(string: )!"https://api.cloudsight.ai/v1/images"
            var request = URLRequest(url: url2)
            request.httpMethod = "POST"
            request.addValue("application/json""Content-Type", forHTTPHeaderField: )
            request.addValue("CloudSight API KEY", forHTTPHeaderField: "Authorization")
            request.httpBody = \strBase64."{\n \"image\": \"()\",\n \"locale\": \"us_en\" \n}"data(using: .utf8)
            let task = URLSessionshareddataTask..(with: request) { data, response, error in
                ifletlet  response = response,  data = data {
                    let res = JSON(data)
                    print("CLOUD RESPONSE ()"\response)
                    print("CLOUD DATA ()"\res)

                }
                else {
                    printString((describing: error))
                }
            }
      task.resume()

这是我从服务器得到的响应:

CL RESPONSE <NSHTTPURLResponse: 0x1c4238ea0> { URL: https://api.cloudsight.ai/v1/images } { status code: 400, headers {
    "Content-Length" = 0;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Sun, 08 Oct 2017 05:26:32 GMT";
    Server = "nginx/1.8.1";
    "x-request-id" = "8494da71-f589-439f-b67e-c709f2c1cd7b";
    "x-runtime" = "1.448816";
} }
CL DATA null

打印数据而不转换为 JSON 也会打印“nil”。

再次感谢!

4

0 回答 0