Why the trailing slash in the web service is so important?
Asked Answered
F

5

8

I was testing a web service in PHP and Python. The address of the web service was, let's say, http://my.domain.com/my/webservice. When I tested the web service in PHP using that URL everything worked fine. But, when I used the same location but in Python using SOAPpy I got an error.

Below is the code I used to communicate with the web service (Python):

from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice', namespace)
server.myFunction()

The respond I got from the server:

HTTPError: <HTTPError 301 Moved Permanently>

I figure out that if I add a trailing slash to the web service location it works!

from SOAPpy import WSDL
server = SOAPProxy('http://my.domain.com/my/webservice/', namespace)
server.myFunction()

Why the lack of the trailing slash causes the error?

Foxe answered 27/7, 2009 at 15:43 Comment(0)
I
20

They're different URLs. http://my.domain.com/my/webservice implies a file webservice in the my folder. http://my.domain.com/my/webservice/ implies the default document inside the my/webservice folder.

Many webservers will automatically correct such URLs, but it is not required for them to do so.

Immunology answered 27/7, 2009 at 15:46 Comment(0)
E
3

Because the actual server URL is: http://my.domain.com/my/webservice/

The PHP library must be following redirects by default.

Endure answered 27/7, 2009 at 15:45 Comment(0)
M
3

The error is a 301 redirect meaning the you are being redirected to the URL with the slash on the end by the web server.

It seems that PHP will auto follow this redirect and thus not throw the error, whereas Python won't. You will need to do the following:

  1. Try to Connect to the initial URL
  2. Catch any 301 redirect and possibly 302 redirects as well
  3. If there was a redirect then try to connect to that URL instead.

The new URL should be available in the response headers.

HTH.

Macmacabre answered 27/7, 2009 at 15:56 Comment(0)
B
2

[Disclaimer: This is a copy of my answer from here. I know some people don't like this kind of copying, but this explains why the slash is important.]

Imagine you serve a page

http://mydomain.com/bla

that contains

<a href="more.html">Read more...</a>

On click, the user's browser would retrieve http://mydomain.com/more.html. Had you instead served

http://mydomain.com/bla/

(with the same content), the browser would retrieve http://mydomain.com/bla/more.html. To avoid this ambiguity, the redirection appends a slash if the URL points to a directory.

Boutwell answered 27/7, 2009 at 15:50 Comment(0)
E
0

What a SOAP-URL looks like is up to the server, if a slash is necessary depends on the server and the SOAP implementation.

In your case, I assume that the target server is an apache server and the SOAP URL is actually a directory that contains your SOAP handling script. When you access http://my.domain.com/my/webservice on the server, apache decides that the directory is properly addressed as http://my.domain.com/my/webservice/ and sends a 301 redirect.

SOAP uses a http POST, its up to the client to decide if the redirect should be followed or not, I assume that it just doesn't expect one.

Other implementations of SOAP, e.g. Apache Axis in Java have URLs that look like Servlets, e.g. http://domain.com/soap/webservice without slash, in this case the URL without slash is correct, there is no directory that exists anyway.

Axis fails on redirects as well, I think.

Endamoeba answered 27/7, 2009 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.