Python Forum

Full Version: How to resolve 404 returned from web method when not running on localhost
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm calling a webmethod that has been around for a while. When hitting localhost, all is fine and I get a 200 response. Here is soap envelope:

Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetAccountInfo"

<?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>
<GetAccountInfo xmlns="http://tempuri.org/">
<signinID>string</signinID>
<password>string</password>
</GetAccountInfo>
</soap:Body>
</soap:Envelope>

Now when the python code attempts to hit the hosted url, I am getting a 404 response. Here is the python side code I am using:

    def api_login(self, username, password):
        parameters = {'signinID': username, 'password': password}
        args = urllib.urlencode(parameters)

        request = urllib2.Request(self.ApiLoginCheck+"/GetAccountInfo", args)
        request.add_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
        request.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
        response = urllib2.urlopen(request)

        self.last_response_code = response.code
        self.last_response_content = response.read().decode('utf-8')
        if response.code == 200:
            print(response)
            print(self.last_response_content)
            root = ET.fromstring(self.last_response_content)
            xmlns = {'t': 'http://tempuri.org/'}
            emails = root.findall('.//t:accountEmail', namespaces=xmlns)
            for email in emails:
                print(email)

            if email.text == '' or email.text == None:
                return 0
            else:
                return 1
        else:
            return 0
print out value of request just after line 7, try running from browser.
Request format is unrecognized for URL unexpectedly ending in /GetAccountInfo

for a soap web service should I be able to post with urllib in the manner I'm trying, or would I have to build the XML soap envelope and post the request that way?
Definitely not as pretty, or as elegant as I was hoping. I was able to get things working by constructing the whole soap envelope (incl header) and posting that.

   def api_login3(self, username, password):
        SM_TEMPLATE = """<?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>
            <GetAccountInfo xmlns="http://tempuri.org/">
              <signinID>%s</signinID>
              <password>%s</password>
            </GetAccountInfo>
          </soap:Body>
        </soap:Envelope>
                """

        SoapMessage = SM_TEMPLATE % (username, password)

        print(SoapMessage)

        request = urllib2.Request(self.ApiLoginCheck, SoapMessage)
        request.add_header("Host", self.ApiHost)
        request.add_header("User-Agent", "Python post")
        request.add_header("Content-type", "text/xml; charset=\"UTF-8\"")
        request.add_header("Content-length", "%d" % len(SoapMessage))
        request.add_header("SOAPAction", "http://tempuri.org/GetAccountInfo")
        print(request)
        response = urllib2.urlopen(request)

        self.last_response_code = response.code
        self.last_response_content = response.read().decode('utf-8')
        if response.code == 200:
            print(response)
            print(self.last_response_content)
            root = ET.fromstring(self.last_response_content)
            xmlns = {'t': 'http://tempuri.org/'}
            emails = root.findall('.//t:accountEmail', namespaces=xmlns)
            for email in emails:
                print(email)

            if email.text == '' or email.text == None:
                return 0
            else:                
                return 1
        else:
            return 0

For completeness, here are the 2 urls that helped me reach my solution:

Python XML HTTP Post to send a SOAP message

urllib2 — extensible library for opening URLs