如何在Amazon Web Services中快速发布数据? [英] How to Post Data in Amazon Web Services swift?

查看:172
本文介绍了如何在Amazon Web Services中快速发布数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码迅速将数据发布到"Amazon Web Services"中.

I used this code to post data in "Amazon Web Services" in swift.

            let userNameData =  String("prnty").dataUsingEncoding(NSASCIIStringEncoding)!
            let passData =  String("xxx").dataUsingEncoding(NSASCIIStringEncoding)!
            let tokenData =  String("xxxxxx").dataUsingEncoding(NSASCIIStringEncoding)!
            let deviceTypeData =  String("ios").dataUsingEncoding(NSASCIIStringEncoding)!

            Alamofire.upload(
                .POST,
                "https://xxxxx.execute-api.ap-southeast-1.amazonaws.com/dev/webserv",
                headers:["x-api-key":"xxxxxxxxx"],
                multipartFormData: { multipartFormData in

                    multipartFormData.appendBodyPart(data: userNameData, name: "username")
                    multipartFormData.appendBodyPart(data: passData, name: "password")
                    multipartFormData.appendBodyPart(data: tokenData, name: "token")
                    multipartFormData.appendBodyPart(data: deviceTypeData, name: "deviceType")
                },
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .Success(let upload, _, _):
                        upload.responseJSON { response in
                           print(response)

                        }
                    case .Failure(let encodingError):

                    print(encodingError)

                    }
                }
            )

我能够成功通过AWS服务器进行身份验证.似乎数据无法继续我的server.OKClient在Android中正常工作 不获取帖子数据(对于iOS,获取零个POST数据)* 可能是什么错误?

i am able to authenticate with AWS server successfully but. seems to data is not going further my server.it works fine in Android by OKClient not getting post data (getting nil POST data for iOS)* what could be the error?

推荐答案

如果您的服务器希望接收application/json,则可以很好地进行以下操作:

If your server expect to receive application/json, the following works well:

let headers = ["x-api-key": "xxxxxxxxx"]

let parameters = [
    "userNameData": "prnty",
    "passData": "xxx",
    "tokenData": "xxxxxx",
    "deviceTypeData": "ios"
]

Alamofire.request(.POST, "https://xxxxx.execute-api.ap-southeast-1.amazonaws.com/dev/webserv", headers: headers, parameters: parameters, encoding: .JSON)
    .responseJSON { response in
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
}

如果不是,则无需使用encoding参数.

if not, you don't need to use the encoding parameter.

这篇关于如何在Amazon Web Services中快速发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆