How to send a request using alamofire using xml Body

I installed Alamofire in my project, and now here's what I did.

I installed postman and I placed my url and an xml object inside the body, and I got my result.

Here is an image of what I definitely did with the postman

enter image description here

How can I now use Alamofire or SWXMLHash to send as I am sending it with an email

Thanks in advance!

EDIT

I tried this from another question:

Alamofire.request(.POST, "https://something.com" , parameters: Dictionary(), encoding: .Custom({ (convertible, params) in let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest let data = (self.testString as NSString).dataUsingEncoding(NSUTF8StringEncoding) mutableRequest.HTTPBody = data return (mutableRequest, nil) })) .responseJSON { response in print(response.response) print(response.result) } } 

But he sent nothing

This is the log:

Optional ({URL: https://something.com } {status code: 200, headers {Connection = "keep-alive"; "Content-Length" = 349; "Content-Type" = "application / xml"; Date = "Wed, 02 Nov 2016 21:13:32 GMT"; Server = nginx; "Strict-Transport-Security" = "max-age = 31536000; includeSubDomains";}})

RENOUNCEMENT

EDIT

NEVER FORGET PASSWORD parameters, if you do not have a simple add this parameters: Dictionary ()

+4
xml ios swift alamofire swxmlhash
Nov 02 '16 at 17:19
source share
3 answers

Assuming your request is missing valid HTTP headers, the updated request might look like this:

 Alamofire.request(.POST, "https://something.com", parameters: Dictionary() , encoding: .Custom({ (convertible, params) in let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest let data = (self.testString as NSString).dataUsingEncoding(NSUTF8StringEncoding) mutableRequest.HTTPBody = data mutableRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type") return (mutableRequest, nil) })) .responseJSON { response in print(response.response) print(response.result) } } 

So basically you have to add one line

 mutableRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type") 

Update :
Try the same thing, but use responseData or responseString instead of responseJSON , because it is possible that your answer is not JSON

+2
Nov 03 '16 at 11:43
source share

Using Swift 3 and Alamofire 4

  let stringParams : String = "<msg id=\"123123\" reqTime=\"123123\">" + "<params class=\"API\">" + "<param name=\"param1\">123213</param>" + "<param name=\"param2\">1232131</param>" + "</params>" + "</msg>" let url = URL(string:"<#URL#>") var xmlRequest = URLRequest(url: url!) xmlRequest.httpBody = stringParams.data(using: String.Encoding.utf8, allowLossyConversion: true) xmlRequest.httpMethod = "POST" xmlRequest.addValue("application/xml", forHTTPHeaderField: "Content-Type") Alamofire.request(xmlRequest) .responseData { (response) in let stringResponse: String = String(data: response.data!, encoding: String.Encoding.utf8) as String! debugPrint(stringResponse) } 
+6
Jun 07 '17 at 6:37
source share

With Swift 3 and Alamofire 4, you will create a custom ParameterEncoding . Like any other object encoded in XML, SOAP messages can use this parameter encoding, as in the following example. Other XML body encodings can be created in a similar way (check the line for urlRequest.httpBody = ... ):

 struct SOAPEncoding: ParameterEncoding { let service: String let action: String func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest { var urlRequest = try urlRequest.asURLRequest() guard let parameters = parameters else { return urlRequest } if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil { urlRequest.setValue("text/xml", forHTTPHeaderField: "Content-Type") } if urlRequest.value(forHTTPHeaderField: "SOAPACTION") == nil { urlRequest.setValue("\(service)#\(action)", forHTTPHeaderField: "SOAPACTION") } let soapArguments = parameters.map({key, value in "<\(key)>\(value)</\(key)>"}).joined(separator: "") let soapMessage = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" + "<s:Body>" + "<u:\(action) xmlns:u='\(service)'>" + soapArguments + "</u:\(action)>" + "</s:Body>" + "</s:Envelope>" urlRequest.httpBody = soapMessage.data(using: String.Encoding.utf8) return urlRequest } } 

And then use it like this:

 Alamofire.request(url, method: .post, parameters: ["parameter" : "value"], encoding: SOAPEncoding(service: "service", action: "action")) 
+3
Dec 31 '16 at 21:56
source share



All Articles