0

I have a prepared URL https://my.comp.com/Online/SendToken?token= that I want to append the parameter value 123 to.

What I have tried so far:

from urllib.parse import urljoin

urljoin('https://my.comp.com/Online/SendToken?token=','123')
# prints 'https://my.comp.com/Online/123'

What I want to get is:

'https://my.comp.com/Online/SendToken?token=123'

I know I can use + to concatenate two strings, but I want to use the urljoin for it.

1 Answer 1

1

Issue

If you only pass a query-parameter value, it will not join them as expected.

Second argument

The second argument is interpreted as path, so it needs to be a valid

  • complete path,
  • path segment (may include query-parameter or fragment)
  • query (prefixed with ?
  • fragment (prefixed with #)

See the docs for urljoin, use the first 2 examples:

from urllib.parse import urljoin

# the complete query-parameter (question-mark, key, value)
urljoin('https://my.comp.com/Online/SendToken?token=','?token=123')
# 'https://my.comp.com/Online/SendToken?token=123'

# the complete path-segment including query
urljoin('https://my.comp.com/Online/SendToken?token=','SendToken?token=123')
# 'https://my.comp.com/Online/SendToken?token=123'

# a fragment (denoted by hash-symbol) will also work, but not desired
urljoin('https://my.comp.com/Online/SendToken?token=','#123')
# 'https://my.comp.com/Online/SendToken?token=#123'

See also Python: confusions with urljoin

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.