api - 得到响应后得到 400,尽管 url 格式正确
问题描述
执行获取 url 如下
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 [/apit/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>
这是为什么?
解决方案
查询参数中的引号似乎导致服务器出现错误。尝试这样的事情
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)
尝试并恢复。
推荐阅读
- java - 如何在具有自定义标签的 xml 中添加新行?
- javascript - 选择事件侦听器在 forEach 循环中不起作用
- php - PHP数据库更新中如何使用SQL累计
- python-3.x - 如果我不点击 google chrome 图标,Driver.get("url") 会抛出超时错误
- php - laravel 如何自动加载类并在不启动它的情况下使用它们?
- python - 连续按4次后几秒不播放音效?
- reactjs - 我在合同中返回一个数组。如何使用 react 和 web3 在我的 app.js 中接受它?
- python-3.x - DataFrame:我如何按 Z 分组并计算 Y 范围内的平均 X
- sql - 查找用于将财政季度结果与上一年进行比较的查询
- css - -webkit-transform:rotateY 在 Mac OS 上失败