Way to set the entire path of a URL without changing the parameters/files to upload/POST data etc


I’ve run into a need to edit the path of a passed-in juce::URL that may contain parameters and/or files to upload. I’ve found that, while I can change the sub-path easily with withNewSubPath, I can’t do anything about the domain or scheme (and there’s no easy way to copy the parameters & files to a new URL). I fixed it by adding a new method to the URL class and was advised to post it here:

(we are on JUCE 4.2.1)

(in juce_URL.h)

/** Returns a new version of this URL that uses a different path.
    (but keeps the parameters, POST data, upload files etc.)

 E.g. if the URL is "http://www.xyz.com/foo?x=1" and you call this with
 "https://www.foo.com/bar", it'll return "https://www.foo.com/bar?x=1".
URL withNewPath (const String& newPath) const;

(in juce_URL.cpp)

URL URL::withNewPath (const String& newPath) const
    URL u (*this);
    u.url = newPath;
    return u;


Thanks, sounds like a sensible and simple request, will add it!


Much appreciated!