Bottom Page

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
Quote
#2
print out value of request just after line 7, try running from browser.
Quote
#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?
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  wrap_text with openpyxl. How to use documentation to resolve deprecation warning? curranjohn46 4 252 Oct-09-2019, 01:04 PM
Last Post: curranjohn46
  Not all data returned from sys.argv ecdhyne 2 257 Sep-05-2019, 08:27 PM
Last Post: buran
  I always get 'None' returned. Confused. What did I miss? jpezz 2 288 Apr-07-2019, 10:06 AM
Last Post: jpezz
  problems when running a signrawtransaction method wectri 1 436 Dec-04-2018, 04:53 PM
Last Post: Larz60+
  function wanted: resolve relative path Skaperen 4 579 Sep-06-2018, 01:52 AM
Last Post: Skaperen
  how to do comparison for the value returned by os.system jash 2 1,038 Nov-11-2017, 11:09 AM
Last Post: snippsat
  Class method returning multiple values,dont know which is returned when.. ujjwalrathod007 4 1,982 Oct-03-2016, 08:29 PM
Last Post: ujjwalrathod007

Forum Jump:


Users browsing this thread: 1 Guest(s)