for example https://new.loan.com/loan?loanId=9307
I want the loan id 9307 & use it to navigate to another url containing this id : http://my.my.loan.com/loan?loanId=9307
You could use the .split() function to separate strings into parts. The parts start at zero, so you would want the part equal 1.
def myNum = url.split(’=’)[1]
1 Like
thank you it worked :)))
What to do if you have 2 or more parameters in the query part of the URL like:
http://my.my.loan.com/loan?loanId=9307&foo=FOO&bar=BAR#baz
For parsing a complex string of URL, java.net.URL
class is helpful.
Sample script
import java.net.URL
URL url = new URL("http://my.my.loan.com/loan?loanId=9307&foo=FOO&bar=BAR#baz")
println "protocol:${url.getProtocol()}"
println "host: ${url.getHost()}"
println "path: ${url.getPath()}"
println "query: ${url.getQuery()}"
println "ref: ${url.getRef()}"
String query = url.getQuery()
List<String> kvPairs = query.split('&') as List
kvPairs.each { pair ->
List<String> strings = pair.split('=') as List
println "key: ${strings[0]}, value: ${strings[1]}"
}
Output:
protocol:http
host: my.my.loan.com
path: /loan
query: loanId=9307&foo=FOO&bar=BAR
ref: baz
key: loanId, value: 9307
key: foo, value: FOO
key: bar, value: BAR
1 Like