How to pass '/' character in the GET request?

Viewed 336

I have an api :

http://localhost:8080/mylocalserver/#/reports/{parameter1}/{parameter2}

now the first parameter is parameter1 = "12345/EF" and the second parameter is parameter2 = "Text"

I am using Spring Rest Controller for creating the api. When the request is made with the following above parameters it shows Request Not Found error.

I have tried encoding and decoding the Parameters using URLEncoder.encode and also with UriUtils.encode(param, StandardCharsets.UTF_8.name()) but still getting the same error.

Below is the code which I tried.

Suppose I got two variables from User Input parameter1 and parameter2

Now I create a URL with the following parameter

private void createApi(String parameter1, String parameter2) {
    try {
      String uri = " http://localhost:8080/mylocalserver/#/reports/"+encode(parameter1)+"/"+encode(parameter2)+" ";

      ServletRequestAttributes requestAttributes =
          (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      HttpServletRequest request = requestAttributes.getRequest();
      HttpServletResponse response = requestAttributes.getResponse();
      request.getRequestDispatcher(uri).forward(request, response);
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    }
  }

And My encode method is :

public String encode(String param) {
    try {
     UriUtils.encode(param, StandardCharsets.UTF_8.name())
      }
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    }
    return param;
  }

Note : I am using Tomcat 8.5

1 Answers

Your encode method is broken:

There is missing return statement.

public String encode(String param) {
    try {
        //missing return at this line in your code
        return UriUtils.encode(param, StandardCharsets.UTF_8.name())
      }
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    }
    return param;
  }

And BTW you can make this method static.

Related