title: How to create soap header with Python suds library?
body: <p>I need to call a SOAP service with a message like this:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub"&gt;
     &lt;soapenv:Header&gt;
        &lt;sub:auth&gt;
           &lt;token&gt;?&lt;/token&gt;
           &lt;!--Optional:--&gt;
           &lt;user_id&gt;?&lt;/user_id&gt;
           &lt;!--Optional:--&gt;
           &lt;user_token&gt;?&lt;/user_token&gt;
        &lt;/sub:auth&gt;
     &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
        &lt;sub:customer_logos_pull&gt;
           &lt;!--Optional:--&gt;
           &lt;language&gt;?&lt;/language&gt;
           &lt;!--Optional:--&gt;
           &lt;limit&gt;?&lt;/limit&gt;
           &lt;!--Optional:--&gt;
           &lt;options_utc&gt;?&lt;/options_utc&gt;
        &lt;/sub:customer_logos_pull&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I've got some php example code, which sets the headers as follows (and which works perfectly well):</p>
  
  <p>auth = array();
  $auth['token'] = 'xxx';
  if ($auth) {
      // add auth header
      $this->clients[$module]->__setSoapHeaders(
          new SoapHeader(
              $namespace, 
              'auth', 
              $auth
          )
      );
  }</p>
  
  <p>I now construct the (empty) body and the header as follows with the <a href="https://fedorahosted.org/suds/" rel="nofollow">Python suds lib</a>:</p>
  
  <pre><code>from suds.client import Client
  from suds import WebFault
  
  client = Client(url='https://example.com/sub.wsdl')
  
  auth = client.factory.create('auth')
  auth.token = 'xxx'
  client.set_options(soapheaders=auth)
  
  customerLogosPull = client.factory.create('customer_logos_pull')
  result = client.service.customer_logos_pull(customerLogosPull)
  </code></pre>
  
  <p>but this gives me a <code>not well-formed (invalid token)</code> message. When turning on logging I find this to be the message:</p>
  
  <pre><code>DEBUG:suds.mx.core:processing:
  (Content){
     tag = "auth"
     value =
        (auth){
           token = "xxx"
           user_id = None
           user_token = None
        }
     type = &lt;Element:0x10ff8c950 name="auth"&gt;
     &lt;Complex:0x10ff8cbd0&gt;
        &lt;Sequence:0x10ff8cc50&gt;
           &lt;Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
           &lt;Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
           &lt;Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
        &lt;/Sequence&gt;
     &lt;/Complex&gt;
  &lt;/Element&gt;
   }
  </code></pre>
  
  <p>It looks pretty fine by me, but it also gives a <code>not well-formed (invalid token)</code>. seeing that the <a href="https://fedorahosted.org/suds/wiki/Documentation#SOAPHEADERS" rel="nofollow">suds docs has 3 examples</a> on how to pass in soap headers, I tried the other two as well:</p>
  
  <pre><code>&gt;&gt;&gt; token = client.factory.create('auth.token')
  &gt;&gt;&gt; token.set(TOKEN)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  AttributeError: token instance has no attribute 'set'
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&gt;&gt;&gt; client.set_options(soapheaders={'auth': {'token': 'xxx'}})
  &gt;&gt;&gt; customerLogosPull = client.factory.create('customer_logos_pull')
  &gt;&gt;&gt; result = client.service.customer_logos_pull(customerLogosPull)
  </code></pre>
  
  <p>which gives this content in the logs, and still a <code>not well-formed (invalid token)</code>:</p>
  
  <pre><code>(Content){
     tag = "auth"
     value =
        {
           token = "xxx"
        }
     type = &lt;Element:0x106049290 name="auth"&gt;
     &lt;Complex:0x106049510&gt;
        &lt;Sequence:0x106049590&gt;
           &lt;Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
           &lt;Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
           &lt;Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" /&gt;
        &lt;/Sequence&gt;
     &lt;/Complex&gt;
  &lt;/Element&gt;
   }
  </code></pre>
  
  <p>Does anybody know how I can correctly set the token in the header using Python? All tips are welcome!</p>