31

My program throws this exception online,I know the reason why it is wrong. My question is how do I find the wrong place,The Java cannot catch the location of this exception. How do I get additional information about this exception,For example, the API for this error requested address.

The error message is as follows:

2019-01-18  07:49:23.076 [http-nio-127.0.0.1-8081-exec-96] INFO  org.apache.coyote.http11.Http11Processor - Error parsing HTTP request header
 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:484)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:684)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
Leandy
  • 595
  • 1
  • 5
  • 14
  • 2
    Seems like there are debug statements in that class https://github.com/apache/tomcat/blob/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java#L386 . You should be able to see what's wrong when you enable debug logger with name `org.apache.coyote.http11.Http11InputBuffer` – rkosegi Jan 21 '19 at 10:31
  • Since it is online, the debug mode is not turned on. – Leandy Jan 21 '19 at 11:04

5 Answers5

26

If you use an upper version of Tomcat 8.5 it throws this exception if the URL path contains '[' and ']'. For older versions, it works.

Dani
  • 291
  • 3
  • 9
  • 1
    Yes, this error occurs because the parameters in the path have special characters.so,we should 'encodeURL' your parameters before passing them.or replace the GET with POST methods. – Leandy Mar 29 '19 at 03:53
  • Ya, in my case, I was missing encodeURIComponent(). I used JSON.Stringify() on my object, but that was not sufficient. When I saw this, I immediately had a D'oh moment. – Jacob Barnes Jun 21 '22 at 15:43
14

The workaround would be adding below attributes to http connector port in tomcat server.xml file

relaxedPathChars="[]|" relaxedQueryChars="[]|{}^\`"<>"
thatguy
  • 21,059
  • 6
  • 30
  • 40
4

The error message says "Invalid character found in the request target". This means the HTTP client sent a request for a resource that had an invalid characters in it. The server can not parse the request because the request did not adhere to the HTTP protocol specifications.

This is a client problem. Fix the client.

If it is a public server, maybe someone is trying to break in by sending malformed requests (it's common).

Torben
  • 3,805
  • 26
  • 31
  • The problem is an exception thrown by the online server,I did not have this problem in the local test – Leandy Jan 21 '19 at 11:00
3

I encountered the same error when sending location of a file in a AJAX GET request.

Since the location had characters which are not recognized. I.e. "C:///" etc, the error was thrown.

The use of encodeURIComponent helped me fix the issue since it encodes the component.

When you pass the location make sure you add those inside "encodeURIComponent" method. In my case:

 $.ajax({
        type: "GET",
        url: 'removeFile?removeFilePath=' + encodeURIComponent("C:///YO/Ed/PO/")
        data: {},
        dataType: 'json',
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
2

Used Tomcat v8.5, below update in server.xml works for me since '<>' is not allowed

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" relaxedPathChars="[]|" relaxedQueryChars="[]|{}^`'"/>
Hailin Tan
  • 989
  • 9
  • 7