如何使用Alamofire发送带有gziped内容的HTTP POST请求?

时间:2022-08-22 17:50:36

I'm working on a iOS app to post about 2000 array of objects to server. I'm doing an experiment. I used let param: [String: AnyObject] = [ "OwnerEmail": document.fromOwner!.email as AnyObject, "UTCTimeStamp": document.utcTimeStamp as AnyObject, "Latitude": document.latitude as AnyObject, "Longitude": document.longitude as AnyObject, ... ]

我正在开发一个iOS应用程序,向服务器发布大约2000个对象数组。我正在做一个实验。我使用了param:[String:AnyObject] = [“OwnerEmail”:document.fromOwner!.email as AnyObject,“UTCTimeStamp”:document.utcTimeStamp as AnyObject,“Latitude”:document.latitude as AnyObject,“Longitude”:document .longitude为AnyObject,...]

customAlamofireManager.request( ServerURL method: .post, parameters: param, encoding: JSONEncoding.default, headers: headers ) to post 1 object. But now I'm posting 2000 or more, I'm thinking using gzip to compress the array of objects and then upload to check network efficiency. But I don't know how to do that using Alamofire. I found this link
However Alamofire has no enum ParameterEncoding in the latest version now. Some other links suggest use Alamofire.upload. Any help is appreciated.

customAlamofireManager.request(ServerURL方法:.post,参数:param,编码:JSONEncoding.default,headers:headers)发布1个对象。但是现在我发布2000或更多,我正在考虑使用gzip来压缩对象数组,然后上传以检查网络效率。但我不知道如何使用Alamofire。我找到了这个链接但是Alamofire现在没有最新版本的enum ParameterEncoding。其他一些链接建议使用Alamofire.upload。任何帮助表示赞赏。

1 个解决方案

#1


0  

You can use this below code to pass the parameters for POST request using Alamofire SDK:

您可以使用以下代码使用Alamofire SDK传递POST请求的参数:

 let param: [String: AnyObject] = [
        "OwnerEmail": document.fromOwner!.email as AnyObject,
        "UTCTimeStamp": document.utcTimeStamp as AnyObject,
        "Latitude": document.latitude as AnyObject,
        "Longitude": document.longitude as AnyObject,
        ...
    ]
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    do{
        let data = try JSONSerialization.data(withJSONObject: param, options: .prettyPrinted)
        request.httpBody = data

        Alamofire.request(request).responseJSON { (response) in


            print(response)

        }

    } catch
        {
       }

This will works for you to resolve your above issue.

这将有助于您解决上述问题。

#1


0  

You can use this below code to pass the parameters for POST request using Alamofire SDK:

您可以使用以下代码使用Alamofire SDK传递POST请求的参数:

 let param: [String: AnyObject] = [
        "OwnerEmail": document.fromOwner!.email as AnyObject,
        "UTCTimeStamp": document.utcTimeStamp as AnyObject,
        "Latitude": document.latitude as AnyObject,
        "Longitude": document.longitude as AnyObject,
        ...
    ]
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    do{
        let data = try JSONSerialization.data(withJSONObject: param, options: .prettyPrinted)
        request.httpBody = data

        Alamofire.request(request).responseJSON { (response) in


            print(response)

        }

    } catch
        {
       }

This will works for you to resolve your above issue.

这将有助于您解决上述问题。