Swift 2.0 with soap request with xml Alamofire parameters

I want to query this web service example: http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl

I need to send one parameter "countryCode". I do not know how to do this with alamofire. And how do I get the answer to parse the xml result.

Here's how I did it at the postman, but I want to know how to do the same in quick. enter image description here

Thank you for your help.

+5
source share
4 answers

Try

  • Executes a POST request
  • URL = http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl
  • Parameters are sent to the encoding section
  • coding: .Custom =>
  • Created by mutbleRequest
  • a. You can directly use URL or b. New URL
  • Important is mutableRequest.HTTBody, where the soap message is placed with the necessary values ​​(type String)
  • The headers may differ depending on the web service and API, in one case there may be a username / password / token or only authorization

  • line → "Content-Type": or "Content-Length" depends on the situation.

  • SWXMLHash.parse (response.data!) Is designed to handle the response, you can see more in [AlamoFire / Usage / Processing of responses] ( https://github.com/Alamofire/Alamofire )

    Alamofire.request(.POST, "http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl", parameters: nil, encoding: .Custom({ (convertible, params) in //a. let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest // or //b. mutableRequest.URL =NSURL(string: theUrlString) let mutableReques02 = NSMutableURLRequest(URL: theURL!) // //mutableReques02.HTTPBody =.... mutableRequest.HTTPBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"urn:sap-com:document:sap:rfc:functions\"><SOAP-ENV:Body> <hs:GetHolidaysAvaible> <hs:countrycode> UnitedStates</hs:countrycode> </hs:GetHolidaysAvaible> </SOAP-ENV:Body></SOAP-ENV:Envelope>".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) return (mutableRequest, nil)}), headers: ["Username": "login", // Login API "Password": "password" , // Password API "AuthenticatedToken" : "35432", // Authenticated API "Authorization": "Basic nasiouynvo8eyt829409", // Authenticated API "Accept" : "text/xml", "Content-Type" : "text/xml; charset=UTF-8", // Type content and charset accept "Accept-Charset" : "UTF-8",]) .responsePropertyList { response in let xml = SWXMLHash.parse(response.data!) print(xml) } 

Note: my english is not very good

+2
source

I wrote a block for mapping XML into objects called XMLMapper . (uses the same technique as ObjectMapper )

You can use Requests to create SOAP requests.

First create your own SOAPMessage :

 class HolidayServiceMessage: SOAPMessage { var countryCode: String? override func mapping(map: XMLMap) { super.mapping(map: map) countryCode <- map["m:countrycode"] } } 

Then create SOAPEnvelope as follows:

 let soapMessage = HolidayServiceMessage(soapAction: "GetHolidaysAvaible", nameSpace: "http://holidaywebservice.com/HolidayService_v2") soapMessage.countryCode = "UnitedStates" let soapEnvelope = SOAPEnvelope(soapMessage: soapMessage) 

Complete the submission of the SOAP request using Alamofire:

 Alamofire.request("http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl", method: .post, parameters: soapEnvelope.toXML(), encoding: XMLEncoding.soap(withAction: "http://holidaywebservice.com/HolidayService_v2#GetHolidaysAvaible")) 

You can map an XML response to fast objects using the XMLMappable protocol.

+2
source

I got this from github:

Alamofire does not have native SOAP or XML support, so you will have to write your own. Fortunately, it's easy to create a response serializer, but XML parsing is up to you.

Regarding options, take a look at the documentation and see which built-in encoding matches your use case.

0
source

Take a look at SOAPEngine. It supports Swift and seems to be maintained in good condition.

-3
source

Source: https://habr.com/ru/post/1234900/


All Articles