Python Forum
How to resolve 404 returned from web method when not running on localhost
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to resolve 404 returned from web method when not running on localhost
#1
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
Reply
#2
print out value of request just after line 7, try running from browser.
Reply
#3
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?
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  How to access values returned from inquirer cspower 6 696 Dec-26-2023, 09:34 PM
Last Post: cspower
  [split] How to resolve version conflicts in Python? atonalwilson 1 950 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 873 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 821 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,894 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  SQLAlchemy Object Missing when Null is returned Personne 1 1,676 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,528 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 2,946 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,126 Aug-18-2021, 05:27 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020