0

I have one request which response is displayed as url below. I need to extract user_token value from url and pass to subsequent request. response url: http://example.com?user_token=0c1c59bc-3aaa-40f1-b978-7172de09a27f&m_id=9999&code=200&is_register=false&M=SUCCESS

i want to extract user_token from it and want to pass it to subsequent request, need solution in java code not java script.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    It is unclear what you are talking about. Do you want to extract the token from the url-String or do you want an endpoint that can use the query parameter? – Turing85 Aug 07 '20 at 20:05
  • 2
    I would suggest looking here: [https://stackoverflow.com/questions/5902090/how-to-extract-parameters-from-a-given-url](https://stackoverflow.com/questions/5902090/how-to-extract-parameters-from-a-given-url) – Snorik Aug 07 '20 at 20:09

2 Answers2

0

You can do this:

String getTokenId(String url){

String[] splitUrl = url.split("user_token=");
String tokenId = "";

if(splitUrl.length >1){

for(int i =0; i < splitUrl[1].length(); i++){
    if(splitUrl[1].charAt(i) == '&'){
        tokenId = splitUrl[1].substring(0,i);
        break;
    }
}

}

return  tokenId;
}

But if you know the exact length of the token, it could be a search.

Otid0
  • 239
  • 1
  • 6
0

Maybe what you need is request.getParameter("user_token"). You can do this in servlet for example

  • Note, that request is a parameter of a servlet method like doGet or doPost –  Aug 07 '20 at 20:18