Parsing an Alamofire request in swift

There are a lot of reasons for parsing an Alamofire request in swift. I found this procedure very useful if you need to get some response from an XML after an Alamofire REST request.

All you need is Alamofire and SwiftyXMLParser (you can parse it manually with the native swift code but using this pod will save you a lot of time). Once you’ve loaded you pods you can proceed by simply declaring a function and follow the code that you can find below.

import Foundation
import Alamofire
import SwiftyXMLParser
func alamofireRequest(register:Bool, _ completion : @escaping (Bool) -> ()){
		let parameters = [
			"name"        : "Tim",
			"second_name"      : "Cook",
			"company"     : "Apple" ] as [String : Any]

		let  url = ""
		Alamofire.request(url, method: .get, parameters: parameters).responseData { response in
			if( response.result.isSuccess){
				if let data =
					let xml = XML.parse(data)
					if let result_code   = xml["root", "result", 0 ].attributes["work_here"] {
						if(result_code == "1"){

In this example the Alamofire.request will contact the url “” and send the parameters declared before as GET (so the final result will be something like

As soon as the code behind that page will provide the xml page we are able to parse it and provide a response. In the code below you will see the XML generated by the page that we have just loaded.

  <result code="1" msg="He works here"/>

In this case the XML parameters “code” is worth “1” so we can comple the function with a “true” answer (see the completion(true) part).

Now you are able to load and parse every XML file without any issues.

Stay Tuned


Site Footer