1

Python doesn't seem to have a function to do this - or documentation that an existing function can do this - and I am a bit lost combing through RFCs.

I have a url that I have parsed with urlparse:

url = "https://example.com/path/to;jsessionid=foo?foo=bar&biz=bang"

It has both "params" and a "query string"

I need to decode/recode the params (jsessionid=foo)

For decoding, I tried urlparse.parse_qs and it works fine. Looking at the code, it appears to use ; as primary delimiter in a nested list comprehension:

def parse_qsl(qs
    ...
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]

In terms of rebuilding the params though, it seems that I'd need to re-implement urllib.urlencode and just change the delimiter from & to ;.

That seems a bit odd to me, and I feel like I must be missing something obvious.

edit: After thinking, I could probably just do params = params.replace("&", ";") as the key/value payloads shouldn't be affected since the RFC has both characters reserved.

3
  • Not sure if such standard exists. Could you point any reference to url params other than query strings? Commented Mar 28, 2017 at 23:23
  • This is not a standalone standard, it's part of RFC for URIs. They're commonly called "url paramaters" or "path segment parameters". RFC 3986 discloses this in detail under "3.3 Path". Almost every RFC that improves upon or extends 3986 includes it as well. Commented Mar 29, 2017 at 4:44
  • If query string order for ; part matters for your case, you can still use ordered dict in that answer. However, yes I think simple replace would help. Finally you would join url + two parts all together. Commented Mar 29, 2017 at 5:04

0

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.