Search code examples
iosswiftxcodetwilioalamofire

AlamoFire for twilio


I am trying to send a post request for the twilio api using swift and Alamofire, but I am having some issues.

This is the function that should send the text:

func sendSMS() {
    let parameters = [
        "To": "+12036044632",
        "From": "+13852322267",
        "Body": "Hi daddy"
        ]
    
    Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters).response { response in
        print(response.0)
        print(response.1)
        print(response.2)
        print(response.3)
    }
}

When I run this, I get this printed in the console:

`Optional(<NSMutableURLRequest: 0x7a844a30> { URL: https://AC11ed62fdb971a8f56d9be531a5ce40c2:[email protected]/2010-04-01/Accounts/AC11ed62fdb971a8f56d9be531a5ce40c2/Messages })
Optional(<NSHTTPURLResponse: 0x7a9f91f0> { URL: https://AC11ed62fdb971a8f56d9be531a5ce40c2:[email protected]/2010-04-01/Accounts/AC11ed62fdb971a8f56d9be531a5ce40c2/Messages } { status code: 401, headers {
    "Access-Control-Allow-Credentials" = true;
    "Access-Control-Allow-Headers" = "Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since";
    "Access-Control-Allow-Methods" = "GET, POST, DELETE, OPTIONS";
    "Access-Control-Allow-Origin" = "*";
    "Access-Control-Expose-Headers" = ETag;
    Connection = "keep-alive";
    "Content-Length" = 327;
    "Content-Type" = "application/xml";
    Date = "Tue, 02 Aug 2016 02:21:01 GMT";
    "Twilio-Request-Duration" = "0.004";
    "Twilio-Request-Id" = RQ36001aed85114ea18c7eac4f20caaf59;
    "Www-Authenticate" = "Basic realm=\"Twilio API\"";
    "X-Powered-By" = "AT-5000";
    "X-Shenanigans" = none;
} })

Optional(<3c3f786d 6c207665 7273696f 6e3d2731 2e302720 656e636f 64696e67 3d275554 462d3827 3f3e0a3c 5477696c 696f5265 73706f6e 73653e3c 52657374 45786365 7074696f 6e3e3c43 6f64653e 32303030 333c2f43 6f64653e 3c446574 61696c3e 596f7572 20416363 6f756e74 53696420 6f722041 75746854 6f6b656e 20776173 20696e63 6f727265 63742e3c 2f446574 61696c3e 3c4d6573 73616765 3e417574 68656e74 69636174 696f6e20 4572726f 72202d20 4e6f2063 72656465 6e746961 6c732070 726f7669 6465643c 2f4d6573 73616765 3e3c4d6f 7265496e 666f3e68 74747073 3a2f2f77 77772e74 77696c69 6f2e636f 6d2f646f 63732f65 72726f72 732f3230 3030333c 2f4d6f72 65496e66 6f3e3c53 74617475 733e3430 313c2f53 74617475 733e3c2f 52657374 45786365 7074696f 6e3e3c2f 5477696c 696f5265 73706f6e 73653e>)
nil`

I then do not get a text message, and also on the Twilio console, it does not count and does not charge me. Am I getting some kind of error? What am I doing wrong?

Note: I understand the security risks of having the auth in the url, and that is not the fix I am looking for so please do not post or comment about that. Thanks


Solution

  • Instead of:

    Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters).response { response in
        print(response.0)
        print(response.1)
        print(response.2)
        print(response.3)
    }
    

    Try this:

    Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters)
        .authenticate(user: user, password: password)
        .responseJSON { response in
            let response = String(response.result.value)
            print(response)
    }
    

    You have to authenticate correctly into Twilio using Alamofire, and that is what ".authenticate" is for. Change the variable user to your account SID, and change the variable password to your auth token. Reference https://www.twilio.com/console/account/settings to find your account SID and auth token.

    You can then manipulate the response string however you'd like.

    Hope this helps.