The request of a upload task should not contain a body or a body stream, use `upload(for:fromFile...

升级Xcode 15 后,上传图片到 aws s3 xcode 控制台打印如下警告

  • The request of a upload task should not contain a body or a body stream, use upload(for:fromFile:), upload(for:from:), or supply the body stream through the urlSession(_:needNewBodyStreamForTask:) delegate method

  • 大概意思:上传任务的请求不应包含正文或正文流

问题代码如下,虽然警告但不影响使用。

        var request = URLRequest(url: url)
        request.httpMethod = "PUT"
        request.httpBody = imageData;
        request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
        request.timeoutInterval = 60
        AF.upload(InputStream(), with: request).uploadProgress { progress  in
            
            progressBlock(progress)
        }.validate().response { AFDataResponse in
           
       }

更改后,问题解决

        var headers = AF.sessionConfiguration.headers;
        headers.add(name: "Content-Type", value: "image/jpeg");

        AF.upload(imageData,
                  to: url,
                  method: .put,
                  headers: headers,
                  requestModifier: {$0.timeoutInterval = 60}
        
        ).uploadProgress { progress in
            
            print(progress);
            progressBlock(progress);
        }.validate().response { AFDataResponse in
            
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容