How to make this curl request using Almofire?

57 views Asked by At

This is my curl url

curl -X POST https://fax.twilio.com/v1/Faxes \
         --data-urlencode "From=+15017122661" \
         --data-urlencode "To=+15558675310" \
         --data-urlencode "MediaUrl=https://www.twilio.com/docs/documents/25/justthefaxmaam.pdf" \
         -u ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token

How can i implement this in Almofire? I am trying like this way.

AF.request(URL(string: "https://fax.twilio.com/v1/Faxes")!, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).authenticate(username: "", password: "").responseJSON { response in

            print(response)
        }

how can i implement data-urlencode "From=+15017122661" in almofire?

1

There are 1 answers

0
Vadim Nikolaev On

Try this code with Alamofire:

if let url = URL(string: "https://fax.twilio.com/v1/Faxes") {

    let parameters = [
        "From": "+15017122661",
        "To": "+15558675310",
        "MediaUrl": "https://www.twilio.com/docs/documents/25/justthefaxmaam.pdf"
    ]

    AF.request(url, method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: nil, interceptor: nil)
        .authenticate(username: "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", password: "your_auth_token")
        .responseJSON { response in
            switch response.result {
            case .success(let json):
                print(json)
            case .failure(let error):
                print(error)
            }
    }
}

I've tested with my creds - it should be work, but I haven't real devices for in/out fax