Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 soap request with Alamofire send xml parameters

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

I need to send one parameter "countryCode". I don't know how can i do that with alamofire. And how i get the response to parse the xml result.

This is how i did in postman, but i want to know how do the same in swift. enter image description here

Thanks for your help.

like image 938
Dasoga Avatar asked Nov 09 '22 02:11

Dasoga


1 Answers

Try it

  • Will make a POST request
  • URL = http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?wsdl
  • The Parameters are sent to the encoding section
  • encoding: .Custom =>
  • Created a mutbleRequest
  • a. You can take directly URL or b. New URL
  • the important is mutableRequest.HTTBody, it is where the soap-message is placed with the necessary values (type String)
  • the Headers can vary from according to the web service and API, in one case can have login / password / token or only authorisation

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

  • SWXMLHash.parse(response.data!) is to handle the response, your can see more in [AlamoFire / Usage / Response Handling ] (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

like image 116
Carlos Parada Avatar answered Nov 15 '22 05:11

Carlos Parada