Search code examples
smalltalkpharo

Add path segment at last part of URL with ZnUrl


I am using Pharo 3 and I want to add a path segment as the last part of an URL for example http://example.com/myapp?key1=param1&key2=param2 and I want to get /myParam added to the last part. With ZnUrl I tried with #addSegment:

(ZnUrl fromString: 'http://example.com/myapp?key1=param1&key2=param2')
  addPathSegment: 'myParam'

but results in

 http://example.com/myapp/myParam?key1=param1&key2=param2

How could I configure the ZnUrl to get?

http://example.com/myapp?key1=param1&key2=param2/myParam

Solution

  • The thing you are describing is not a valid URL:

    URL explanation

    So what you are talking about is not an addition of a path segment, but rather string concatenation.

    You can consider doing:

    ZnUrl fromString: 'http://example.com/myapp?key1=param1&key2=param2/myParam' 
    

    or if you get a url from somewhere else,

    (self asString, '/myParam') asUrl
    

    should work too.

    You can also do more magic to get everything to work, but in a first place you have to redesign your URL structure, to fit the standards (if you can influence it)