Reputation: 73
I have found several examples online for how to use Alamofire to get results and JSON and create a JSON object...I'm trying to get the results of an API that returns XML (which I'll parse later) but can't find any good examples that work. I'm using Xcode 8.0, Swift 3, and the most current version of Alamofire (Sep 2016, maybe ver 4.0?)
Here is the code:
import Foundation
import Alamofire
var first_name = ""
var last_name = ""
var URLString = "https://api-equitywerks.rhcloud.com/api/read_ls.xml"
let parameters = [
"Firstname": "\(first_name)",
"Lastname": "\(last_name)", ];
func test() {
Alamofire.request(URLString,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default, headers: nil)
.responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if let data = response.result.value{
print(response.result.value)
print(data)
}
break
case .failure(_):
print(response.result.error)
break
}
}
}
I think I want to replace the .responseJSON chained call with .response, but when I try to do that I get errors. Any ideas? Thanks.
Upvotes: 0
Views: 174
Reputation: 4579
I think you can't call responseJSON
to get the xml response. You need to use responseString
to get the raw string or responseData
to get raw data.
import UIKit
import Alamofire
class TestAlamofireXML: UIViewController {
override func viewDidLoad() {
getData()
}
func getData() {
Alamofire.request(.GET, "https://api-equitywerks.rhcloud.com/api/read_ls.xml", parameters: nil, encoding: .URL, headers: nil)
.validate()
.responseString {
(response) in
switch response.result {
case .Success(let value):
print(value)
case .Failure(let error):
print(error.localizedDescription)
}
}
}
}
Upvotes: 1