4

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

swetansh kumar
  • 475
  • 7
  • 17
  • Please have a look at: https://stackoverflow.com/questions/2992231/slashes-in-url-variables – sven.kwiotek Nov 06 '17 at 13:14
  • 1
    Does it work if you manually escape the slash with `12345%2FEF`? – assylias Nov 06 '17 at 13:16
  • @assylias the request is purely dependent on the user as he can enter any value in the variable therefore I cannot manually replace the values.Therefore I tried encoding the parameters but it didn't work. – swetansh kumar Nov 06 '17 at 13:18
  • Nice explanation and possible duplicate : https://stackoverflow.com/questions/4069002/receive-an-http-400-error-if-2f-is-part-of-the-get-url-in-jboss – Bikramjit Rajbongshi Nov 06 '17 at 13:19
  • @sven.kwiotek the given questions only handle a specific case of this problem. – swetansh kumar Nov 06 '17 at 13:19
  • @swetanshkumar Understood, but does it work if you do it manually? If it does then you know the problem is with the encoding of the URL. If it doesn't then the problem may be with your web service. – assylias Nov 06 '17 at 13:20
  • I need to add a global method for handling these types – swetansh kumar Nov 06 '17 at 13:20
  • @assylias it works when I do it manually but I have to make a dedicated method for handling it in my all apis. – swetansh kumar Nov 06 '17 at 13:22
  • You should post the code that is failing, maybe we would find what's wrong with that. Otherwise we can just write theories. There will likely be some silly mistake, that is usually the case :) – Vlasec Nov 06 '17 at 13:52
  • 1
    @swetanshkumar The thing is that `URLEncoder.encode("12345/EF", UTF_8.toString())` does return `12345%2FEF` as expected - and you say that when you pass that string manually it works fine. So it's not clear what is going on. Please provide a [mcve]. – assylias Nov 06 '17 at 13:58
  • @assylias edited the question with my code – swetansh kumar Nov 07 '17 at 06:22
  • *the request is purely dependent on the user*: then it doesn't identify a resource, and should rather be a query parameter than a path variable. – JB Nizet Nov 07 '17 at 07:29

1 Answers1

2

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.

rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • It would probably clearer if you fixed the error instead of just pointing it out. – assylias Nov 07 '17 at 07:47
  • But that fix I had done already as it was typing mistake by me. Please provide a better method that this one – swetansh kumar Nov 07 '17 at 08:06
  • Then show your real code - this answer solves the problem you describe in your question (except maybe the error handling but which returns the original string if an error is found)... – assylias Nov 07 '17 at 09:34