这里使用的是 Alamofire。我希望这有帮助。我已经对其进行了测试,并且可以正常工作。
func mainRequest(){
var headers: HTTPHeaders = [
"content-type": "application/json"
]
let credentials = "shippo_test_8dc780c66d5dae1c42868596c0b359ba89108df9"
headers["Authorization"] = "ShippoToken \(credentials)"
var parameters:Parameters = [String : Any]()
parameters["name"] = "Shawn Ippotle"
parameters["company"] = "Shippo"
parameters["street1"] = "215 Clayton St."
parameters["street2"] = ""
parameters["city"] = "San Francisco"
parameters["state"] = "CA"
parameters["zip"] = "94117"
parameters["phone"] = "+1 555 341 9393"
parameters["country"] = "US"
parameters["email"] = "shippotle@goshippo.com"
parameters["is_residential"] = "True"
parameters["metadata"] = "Customer ID 123456"
let url = "https://api.goshippo.com/addresses"
Alamofire.request(url, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers)
.responseJSON { (response) in
switch response.result {
case .success(let value):
let swiftyJson = JSON(value)
print ("return as JSON using swiftyJson is: \(swiftyJson)")
case .failure(let error):
print ("error: \(error)")
}
}
}