简体   繁体   中英

How to add headers parameters (-H,-u,-X, -d) in POST request of Alamofire Swift

Below is my curl command which I send via Mac terminal and works well. I need to send it via iOS swift. I am using Alamofire. How can I add these header, user, and data (-H,-u,-X and -d) in Alamofire POST request.

I saw this thread but it is only to add (-H) headers. I also need to send -u (user) and -d (data) in POST request. Please someone help.

curl -H "Accept: application/vnd.conekta-v1.0.0+json" \
 -H "Content-type: application/json" \
 -u key_SOME_API_KEY: \
 -X POST -d '{
  "description":"Stogies",
  "amount": 50000,
  "currency":"USD",
  "details": {
    "name": "Arnulfo Quimare",        
    },
}'

I found with hit and tries :) .It worked for me.

       let key = "API_Key"
    let loginString = NSString(format: "%@", key)
    let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
    let base64LoginString = loginData.base64EncodedStringWithOptions(nil)


    manager.session.configuration.HTTPAdditionalHeaders =
    [
        "Accept": "application/vnd.conekta-v1.0.0+json",
        "Content-type": "application/json",
        "Authorization" : "Basic \(base64LoginString)"
    ]

    let param = [
        "description":"Stogies",
        "amount": 50000,
        "currency":"USD",
        "details": [
            "name": "Arnulfo Quimare",
        ],
    ]

    manager.request(.POST, "https://api.conekta.io/charges", parameters: param )//as? [String : AnyObject] )
        .responseJSON { (request, res, JSONDATA, error) in
            if (res?.statusCode == 200 ){ // SUCCESS

            }
            else if JSONDATA != nil { // SERVER ERROR

            }
            else{ // NO RESPONSE

            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM