2

I've this code that uses Savon that I'm trying get to work.

require "savon"
require "time"

Gyoku.configure do |config|
  config.convert_symbols_to(&:tap)
end

client = Savon::Client.new("https://www.cfhdocmail.com/TestAPI2/DMWS.asmx?WSDL")

response = client.request(:CreateMailing) do
  soap.body = {
    Username: "Username",
    Password: "Password",
    ProductType: "A4Letter",
    IsMono: false,
    IsDuplex: true,
    DespatchASAP: false,
    DespatchDate: Time.parse("2012-04-09"),
    AddressNameFormat: "Full Name",
    ReturnFormat: "JSON"
  }
end

The output looks like this

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="https://www.cfhdocmail.com/LiveAutoAPI/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="https://www.cfhdocmail.com/LiveAutoAPI/">
    <env:Body>
        <ins0:CreateMailing>
            <ins0:Username>Username</ins0:Username>
            <ins0:Password>Password</ins0:Password>
            <ins0:ProductType>A4Letter</ins0:ProductType>
            <ins0:IsMono xsi:nil="true" />
            <ins0:IsDuplex>true</ins0:IsDuplex>
            <ins0:DespatchASAP xsi:nil="true" />
            <ins0:DespatchDate>2012-04-09 00:00:00 +0200</ins0:DespatchDate>
            <ins0:AddressNameFormat>Full Name</ins0:AddressNameFormat>
            <ins0:ReturnFormat>JSON</ins0:ReturnFormat>
        </ins0:CreateMailing>
    </env:Body>
</env:Envelope>

But I want it to look like this.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CreateMailing xmlns="https://www.cfhdocmail.com/LiveAutoAPI/">
      <Username>Username</Username>
      <Password>Password</Password>
      <ProductType>A4Letter</ProductType>
      <IsMono/>true</IsMono>
      <IsDuplex>true</IsDuplex>
      <DespatchASAP>false</DespatchASAP>
      <DespatchDate>2012-04-09 00:00:00 +0200</DespatchDate>
      <AddressNameFormat>Full Name</AddressNameFormat>
      <ReturnFormat>JSON</ReturnFormat>
    </CreateMailing>
  </soap:Body>
</soap:Envelope>

How do I do that?

I'm getting this error at the moment

Server did not recognize the value of HTTP Header SOAPAction: CreateMailing. (Savon::SOAP::Fault)
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
  • 1
    Does [this](http://stackoverflow.com/a/6969379/89876) answer your question? (Especially the `soap.input` part of the answer) – sluukkonen Apr 08 '12 at 23:09

2 Answers2

3

These changes solved my problem.

client.request(:web, "CreateMailer") do |soap|
  soap.input = [
    "CreateMailer", { 
      xmlns: "https://www.cfhdocmail.com/LiveAutoAPI/"
    }
  ]
  soap.element_form_default = :unqualified
  soap.body = {
    Username: "Username",
    Password: "Password",
    ProductType: "A4Letter",
    IsMono: false,
    IsDuplex: true,
    DespatchASAP: false,
    DespatchDate: Time.parse("2012-04-09"),
    AddressNameFormat: "Full Name",
    ReturnFormat: "JSON"
  }
  client.http.headers.delete("SOAPAction")
end

Thanks @sluukkonen for the help.

Community
  • 1
  • 1
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
0

There might be a better way to do it, but the only way I could figure out how to get the XML body exactly how I needed it was to build my own XML. I have a to_xml method in my model and it gets called like soap.body = { to_xml }.

def to_xml
  xml = Builder::XmlMarkup.new(indent: 2)
  xml.instruct!
  xml.Username "Username"
  xml.Password "Password"
end
Peter Brown
  • 50,956
  • 18
  • 113
  • 146