<p>Hi Gophers,</p>
<p>I am learning go by writing a monitor application for the web application I support. Our stack runs on JBoss and we have an endpoint called "status".</p>
<p>In every other language I use I can just send a request to <code>https://ourapp.com/status</code> with the appropriate header set and all is well.</p>
<p>What is confusing me is that in go I have to send the request to <code>https://ourapp.com/status/</code> <strong>note the trailing slash</strong>.</p>
<p>Is this something specific to <code>go</code> that I just need to be aware of or am I seeing some oddity in JBoss/our application specific to how <code>go</code> handles requests?</p>
<pre><code>package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
client := &http.Client{}
//Works fine
req, err := http.NewRequest("GET", "https://ourapp.com/status/", nil)
//resp.Body is empty, resp.ContentLength is 0
//req, err := http.NewRequest("GET", "https://ourapp.com/status", nil)
if err != nil {
fmt.Println(err)
}
req.Header.Set("statuskey", "auth_key_redacted")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
fmt.Println("Response")
fmt.Println(resp.Status)
fmt.Println(resp.ContentLength)
fmt.Println(resp.Header)
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
fmt.Println(buf.String())
}
</code></pre>
<hr/>**评论:**<br/><br/>gohacker: <pre><blockquote>
<p>req.Header.Set("statuskey", "auth_key_redacted")</p>
</blockquote>
<p>Probably issue 4800: <a href="https://github.com/golang/go/issues/4800" rel="nofollow">https://github.com/golang/go/issues/4800</a></p>
<p>Try Go 1.8 beta 2.</p></pre>hipone: <pre><p>(net/http).Client with default transport is not doing any magic behind the scenes with your request - if your url is "<a href="https://ourapp.com/status/" rel="nofollow">https://ourapp.com/status/</a>" then path part of it is "/status/" - accordingly if you used "<a href="https://ourapp.com/status////" rel="nofollow">https://ourapp.com/status////</a>" then path would be "/status////".</p>
<p>If servers expects path to be "/status/" then your Java's http client was adding trailing slash by default. The easiest way to know would be to use some debugging proxy to record http communication and confirm or reject this explanation.</p></pre>xandout: <pre><p>So based on that it seems it isn't <code>Client</code> but the server. </p>
<p>Maybe my Chrome/python stuff is modifying the request. </p></pre>ashwch: <pre><p>You can debug this easily in Python using <code>requests</code>. <code>requests</code> preserves headers and also sets appropriates cookies based on the headers returned by the previous request, but Go won't do any of that.</p>
<pre><code>>>> import requests
>>> r = requests.get('https://ourapp.com/status', headers={<your headers>})
>>> print r.url # print final URL
>>> print r.request.headers
>>> for h in r.history:
print h.request.headers
</code></pre>
<p>If some sort of redirection is happening(based on the Location header returned by the above URL) then the above program will print <code>https://ourapp.com/status/</code>. You can also check the complete redirection history using <code>r.history</code>.</p>
<p>In Go you can do something similar by defining your own <a href="https://golang.org/pkg/net/http/#Client" rel="nofollow"><code>CheckRedirect</code></a> function and printing the headers being sent with each redirect.</p></pre>xandout: <pre><p>Thanks for that tip, there is a <code>302</code> being sent for <code>ourapp.com/status/</code>. Based on my understanding of the docs, <code>net/http</code> will follow up to 10 redirects but it does not appear to be following this one.</p>
<p>Do I need to explicitly state that I would like to follow the 302?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传