在收到GET响应后,出现400错误,尽管URL格式正确。

huangapple go评论99阅读模式
英文:

Getting 400 after a get response, although the url is well formatted

问题

执行以下代码进行get请求:

resp, err := http.Get(path)

在此之前,我打印了path变量,大致如下:

http://localhost:8080/api/history/resources/count?startedAfter="2021-03-06T15%3A27%3A13.894415787%2B0200"

当我点击链接时,它返回200和有效的json响应。

然而,代码本身失败并打印了以下内容:

<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title>Invalid character found in the request target [/api/history/resources/count?startedAfter="2021-03-06T15%3A27%3A13.894415787%2B0200"]. The valid characters are defined in RFC 7230 and RFC 3986</p><p><b>Description</b> The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).</p><p><b>Exception</b></p><pre>java.lang.IllegalArgumentException: Invalid character found in the request target [/api/history/resources/count?startedAfter="2021-03-06T15%3A27%3A13.894415787%2B0200"]. The valid characters are defined in RFC 7230 and RFC 3986
        org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:491)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.base/java.lang.Thread.run(Thread.java:834)
</pre><p><b>Note</b> The full stack trace of the root cause is available in the server logs.</p><hr class="line" /><h3>Apache Tomcat/9.0.36</h3></body></html>

为什么会这样呢?

英文:

Performing a get url as follows

resp, err := http.Get(path)

Beforehand, I print the path variable which is more or less like this

http://localhost:8080/api/history/resources/count?startedAfter=&quot;2021-03-06T15%3A27%3A13.894415787%2B0200&quot;

When I click on the link, it does return 200 and a valid json response.

However the code itself fails and prints:

&lt;!doctype html&gt;&lt;html lang=&quot;en&quot;&gt;&lt;head&gt;&lt;title&gt;HTTP Status 400 – Bad Request&lt;/title&gt;Invalid character found in the request target [&amp;#47;api&amp;#47;history&amp;#47;resources&amp;#47;count?startedAfter=&amp;quot;2021-03-06T15%3A27%3A13.894415787%2B0200&amp;quot;]. The valid characters are defined in RFC 7230 and RFC 3986&lt;/p&gt;&lt;p&gt;&lt;b&gt;Description&lt;/b&gt; The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).&lt;/p&gt;&lt;p&gt;&lt;b&gt;Exception&lt;/b&gt;&lt;/p&gt;&lt;pre&gt;java.lang.IllegalArgumentException: Invalid character found in the request target [&amp;#47;apit&amp;#47;history&amp;#47;resources&amp;#47;count?startedAfter=&amp;quot;2021-03-06T15%3A27%3A13.894415787%2B0200&amp;quot;]. The valid characters are defined in RFC 7230 and RFC 3986
        org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:491)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.base&amp;#47;java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        java.base&amp;#47;java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.base&amp;#47;java.lang.Thread.run(Thread.java:834)
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;Note&lt;/b&gt; The full stack trace of the root cause is available in the server logs.&lt;/p&gt;&lt;hr class=&quot;line&quot; /&gt;&lt;h3&gt;Apache Tomcat/9.0.36&lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;

Why is that?

答案1

得分: 1

你的查询参数中的引号似乎导致了服务器上的错误。尝试像这样修改:

apiURL := "http://localhost:8080/api/history/resources/count"
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
	log.Fatal(err)
}

apiParams := req.URL.Query()
apiParams.Add("startedAfter", "2021-03-06T15:27:13.894415787+0200")
req.URL.RawQuery = apiParams.Encode()

res, err := http.DefaultClient.Do(req)

试试这样,然后回复结果。

英文:

The quotes in your query param seem to be causing the error at the server. Try something like this

apiURL := &quot;http://localhost:8080/api/history/resources/count&quot;
req, err := http.NewRequest(&quot;GET&quot;, apiURL, nil)
if err != nil {
	log.Fatal(err)
}

apiParams := req.URL.Query()
apiParams.Add(&quot;startedAfter&quot;, &quot;2021-03-06T15:27:13.894415787+0200&quot;)
req.URL.RawQuery = apiParams.Encode()

res, err := http.DefaultClient.Do(req)

try that and revert.

huangapple
  • 本文由 发表于 2021年3月6日 21:33:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/66506207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定