我尝试使用swift创建一个来自iphone的帖子到我的ASP API。
public IHttpActionResult POST(FormCollection collection)
{
try
{ //Braintree.Test.Nonce.TransactableMasterCard
var request = new PaymentMethodRequest
{
CustomerId = "thatGuy",
PaymentMethodNonce =collection["payment_method_nonce"],
Options = new PaymentMethodOptionsRequest
{
VerifyCard = true
}
};
Result<PaymentMethod> result = MerchantCredentials.Gateway.PaymentMethod.Create(request);
return Ok();
}
catch
{
return BadRequest("Card not added");
}
}
这就是我的api控制器
func postNonceToServer(paymentMethodNonce: String) {
let paymentURL = NSURL(string: "
http://localhost:3000/api/braintreepaymentmethod ")!
let request = NSMutableURLRequest(URL: paymentURL)
request.HTTPBody =
"payment_method_nonce=\(paymentMethodNonce)".dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data,
response, error) -> Void in
// TODO: Handle success or failure
}.resume()
}
这就是快捷的方式。
我意识到swift正在编码数据,但我不知道如何处理服务器端。