How do I access SOAP headers in a Spyne srpc method?

1.1k Views Asked by At

Forgive me if I am just being thick but the Spyne documentation on headers seems a bit thin and the link to the sample code is broken. I think I found the sample code here but it only seems to access the headers in a listener callback function. I need access to it in the srpc method if possible.

What I would like to do is something like this (this does not work):

class MyRequest(ComplexModel):
    Messages = Array(Unicode)

class MyHeader(ComplexModel):
    TrackingID = ByteArray

class MySoapService(ServiceBase):
    @srpc(MyRequest, _in_header=MyHeader)
    def PostMessages(req, hdr):
        logging.info(u'RECEIVED: {0:s}'.format(hdr.TrackingID))

If it helps, I am trying to replace a service written in .NET that just defines the message headers in the message contract like this:

[MessageContract]
public class MyRequest
{
  [MessageHeader]
  public Guid TrackingID { get; set; }

  [MessageBodyMember]
  public String[] Messages { get; set; }
}
1

There are 1 best solutions below

1
On

You can't read header data from within an srpc function. @srpc is for existing functions that you don't want to touch. You should always use @rpc unless you know you need @srpc

Decorate your function using @rpc. This passes the rpc context (conventionally named ctx) as first argument to your function. Then, ctx.in_header is where you'll find the header data. You can also set ctx.out_header to alter outgoing response headers.

Also, pull requests for docs are pure gold to me :)