i need to insert url path to my url from html form
action parameter by using urllib.parse.urljoin
but urljoin it does not work well in this example , for example
url: http://example.com/login/
form: action="/test"
in web browser after click on submit button it will be like this http://example.com/login/test
but in urljoin
will be like this http://example.com/test
>>> from urllib.parse import urljoin
>>> urljoin('http://example.com/login/','/test')
"http://example.com/test/"
my question about the url that got from action parameters for example : action="/test"
in browser http://example.com/login/test
in urllib http://example.com/test
but if the value equal action="/test/"
in browser and urllib http://example.com/test/
, i need to make like browser when the value dose not end with /
any suggestion ?
thanks
CodePudding user response:
Removing the /
from the second parameter to urljoin
gives the expected result i.e., http://example.com/login/test
from urllib.parse import urljoin
base = 'http://example.com/login/'
path = '/test'
urljoin(base, path.strip('/'))
Note: urljoin should mimic the exact behaviour of browser. So if browser points to http://example.com/login/test
then output of urljoin should too (and vice a versa). Are you sure about the expected result you want?
This question may provide more insights - Python: confusions with urljoin