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.

Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72
  • Not sure if such standard exists. Could you point any reference to url params other than query strings? – hurturk Mar 28 '17 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. – Jonathan Vanasco Mar 29 '17 at 04:44
  • If query string order for `;` part matters for your case, you can still use ordered dict in that [answer](http://stackoverflow.com/a/10765733/1233686). However, yes I think simple replace would help. Finally you would join url + two parts all together. – hurturk Mar 29 '17 at 05:04

0 Answers0