0

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

Jika
  • 365
  • 3
  • 10
  • 4
    There's a possible mistake in the question which makes it confusing. If you click a button in the browser with `/test`, you will get to `http://example.com/test` due to the leading slash. If the button has `./test` you will get to `http://example.com/login/test`. I just checked this both in the browser and in urljoin. – Barak Itkin Sep 10 '21 at 21:09

1 Answers1

1

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