2

I am trying the following API using Alamofire, but this API has multiple "to" fields. I tried to pass an array of "to" emails as parameters. It shows no error but did not send to all emails. API is correct, I tested that from terminal. Any suggestions will be cordially welcomed.

http -a email:pass -f POST 'sampleUrl' from="email@email.com" to="ongkur.cse@gmail.com" to="emailgmail@email.com" subject="test_sub" bodyText="testing hello"

I am giving my code:

class func sendMessage(message:MessageModel, delegate:RestAPIManagerDelegate?) {

    let urlString = "http://localhost:8080/app/user/messages"

    var parameters = [String:AnyObject]()

    parameters = [

        "from": message.messageFrom.emailAddress
    ]

    var array = [String]()

    for to in message.messageTO {

        array.append(to)
    }

    parameters["to"] = array

    for cc in message.messageCC {

        parameters["cc"] = cc.emailAddress;
    }

    for bcc in message.messageBCC {

        parameters["bcc"] = bcc.emailAddress;
    }

    parameters["subject"] = message.messageSubject;
    parameters["bodyText"] = message.bodyText;

    Alamofire.request(.POST, urlString, parameters: parameters)
        .authenticate(user: MessageManager.sharedInstance().primaryUserName, password: MessageManager.sharedInstance().primaryPassword)
        .validate(statusCode: 200..<201)
        .validate(contentType: ["application/json"])
        .responseJSON {

            (_, _, jsonData, error) in

            if(error != nil) {

                println("\n sendMessage attempt json response:")
                println(error!)
                delegate?.messageSent?(false)
                return
            }
            println("Server response during message sending:\n")
            let swiftyJSONData = JSON(jsonData!)
            println(swiftyJSONData)
            delegate?.messageSent?(true)
    }
}
Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19
  • Can you show us the code you've used? And 'that did not work' is not helping us understand the issue, please provide the error. Which API are you trying to call? Does it have any documentation? – Tieme Nov 14 '14 at 14:34
  • I have included my code @Tieme – Md. Najmul Hasan Nov 25 '14 at 05:15

1 Answers1

0

First of all if you created the API yourself you should consider changing the API to expect an array of 'to' receivers instead of multiple times the same parameter name.

As back2dos states it in this answer: https://stackoverflow.com/a/1898078/672989

Although POST may be having multiple values for the same key, I'd be cautious using it, since some servers can't even properly handle that, which is probably why this isn't supported ... if you convert "duplicate" parameters to a list, the whole thing might start to choke, if a parameter comes in only once, and suddendly you wind up having a string or something ...

And I think he's right.

In this case I guess this is not possible with Alamofire, just as it is not possible with AFNetworking: https://github.com/AFNetworking/AFNetworking/issues/21

Alamofire probably store's its POST parameter in a Dictionary which doesn't allow duplicate keys.

Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156
  • Thanks, will try and if successful will let you know. If you closely look my code, you will see that I am passing an array to "to" parameters. Passing array from terminal work but not from app – Md. Najmul Hasan Nov 27 '14 at 06:24
  • I have faced a similar problem now. http://stackoverflow.com/questions/30508534/what-is-the-proper-api-calling-syntax-from-alamofire. I had to change some Alamofire code. – Md. Najmul Hasan May 30 '15 at 02:07