-1

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)

                    }
                }
            )

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?

Matt
  • 74,352
  • 26
  • 153
  • 180
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
  • You might want to remove your `x-api-key` value (and possibly the exact URL) from this code snippet. – Jedidja Mar 22 '16 at 09:54
  • Thanks for reply. but removing "x-api-key" did't work not able to reach to our server – Avijit Nagare Mar 22 '16 at 10:09
  • 3
    You should remove the api key because other people could use it and it might be a security risk ;) – Daehn Mar 22 '16 at 17:46
  • Seems that you are using multipart/form-data for posting data, but does your server expect it? Generally, posting data except file uploading does not use it. http://stackoverflow.com/questions/28526743/how-to-use-alamofire-with-custom-headers-for-post-request – NSR Mar 24 '16 at 02:29
  • @Nori. Do i need aws framework for ios ?i didn"t find any POST request api in that.any help? – Avijit Nagare Mar 24 '16 at 04:21
  • You don't need to use AWS framework called AWS SDK if you wanna post HTTP POST request to your server. According to the documentation of Almofire, if your server expects to receive application/x-www-form-urlencoded, you can use `Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters)`, if it expects to receive application/json, you can use `Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)`. For more information: https://github.com/Alamofire/Alamofire – NSR Mar 24 '16 at 05:21
  • @Nori. look like correct. could you please elaborate answer in details. – Avijit Nagare Mar 24 '16 at 06:06

1 Answers1

2

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)")
        }
}

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

NSR
  • 819
  • 7
  • 20