SOAP web service with soaplib 0.9+

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from soaplib.serializers.primitive import Boolean, String
from soaplib.service import DefinitionBase, rpc
from soaplib.wsgi import Application

from django.http import HttpResponse

# the class with actual web methods
class MySOAPService(DefinitionBase):
    @rpc(String, String, _returns=Boolean)
    def Test(self, f1, f2):
        return True

# the class which acts as a wrapper between soaplib WSGI functionality and Django
class DjangoSoapApp(Application):
    def __call__(self, request):
        # wrap the soaplib response into a Django response object
        django_response = HttpResponse()
        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            django_response.status_code = int(status)
            for header, value in headers:
                django_response[header] = value
        response = super(DjangoSoapApp, self).__call__(request.META, start_response)
        django_response.content = '\n'.join(response)
        return django_response

# the view to use in urls.py
my_soap_service = DjangoSoapApp([MySOAPService], __name__)

More like this

  1. SOAP web service with soaplib 2.0 by treyh 1 year, 4 months ago
  2. soaplib service integration 2 by erny 4 years, 1 month ago
  3. SOAP views with on-demand WSDL generation by chewie 4 years, 9 months ago
  4. Automatically expose soaplib methods in WSDL by AndrewIngram 4 years, 3 months ago
  5. Convert django model into soaplib model, to expose webservices by s.federici 4 years, 4 months ago

Comments

zodman (on November 4, 2010):

for test with suds:

python -c "import suds; print suds.client.Client('http://127.0.0.1:8000/guias/service.wsdl', cache=None).service.Test('1','2')"

#

jonasvp (on November 16, 2010):

Thanks a lot for this! I got it working only after using emilianoheyns' patch from Snippet #979 (I use raw_post_body instead of reconstructing the body by hand from request.POST):

import StringIO
from soaplib.wsgi import Application
from django.http import HttpResponse

from tasks import log_esb_update

class DumbStringIO(StringIO.StringIO):
    def read(self, n):
    return self.getvalue()

# the class which acts as a wrapper between soaplib WSGI functionality and Django
class DjangoSoapApp(Application):
    def __call__(self, request):
    # wrap the soaplib response into a Django response object
    django_response = HttpResponse()
    def start_response(status, headers):
        status, reason = status.split(' ', 1)
        django_response.status_code = int(status)
        for header, value in headers:
            django_response[header] = value

    environ = request.META.copy()
    environ['CONTENT_LENGTH'] = len(request.raw_post_data)
    environ['wsgi.input'] = DumbStringIO(request.raw_post_data)
    environ['wsgi.multithread'] = False

    response = super(DjangoSoapApp, self).__call__(environ, start_response)
    django_response.content = '\n'.join(response)
    return django_response

#

andrewk (on March 2, 2011):

nice snippet, but I catch 403 "CSRF token missing or incorrect." any ideas to solve it?

#

ungenio41 (on October 12, 2011):

Try this to get around csrf protection:

from django.http import HttpResponse
from soaplib.wsgi import Application

class DjangoSoapApp(Application):
    csrf_exempt = True

    def __call__(self, request):
        # wrap the soaplib response into a Django response object
        django_response = HttpResponse()
        def start_response(status, headers):
            status, reason = status.split(' ', 1)
            django_response.status_code = int(status)
            for header, value in headers:
                django_response[header] = value
        response = super(DjangoSoapApp, self).__call__(
            request.META, start_response)
        django_response.content = '\n'.join(response)
        django_response.csrf_exempt = True
        return django_response

#

(Forgotten your password?)