net/http Request requires trailing slash

agolangf · · 455 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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 &#34;status&#34;.</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 ( &#34;bytes&#34; &#34;fmt&#34; &#34;net/http&#34; ) func main() { client := &amp;http.Client{} //Works fine req, err := http.NewRequest(&#34;GET&#34;, &#34;https://ourapp.com/status/&#34;, nil) //resp.Body is empty, resp.ContentLength is 0 //req, err := http.NewRequest(&#34;GET&#34;, &#34;https://ourapp.com/status&#34;, nil) if err != nil { fmt.Println(err) } req.Header.Set(&#34;statuskey&#34;, &#34;auth_key_redacted&#34;) resp, err := client.Do(req) if err != nil { fmt.Println(err) } fmt.Println(&#34;Response&#34;) 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(&#34;statuskey&#34;, &#34;auth_key_redacted&#34;)</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 &#34;<a href="https://ourapp.com/status/" rel="nofollow">https://ourapp.com/status/</a>&#34; then path part of it is &#34;/status/&#34; - accordingly if you used &#34;<a href="https://ourapp.com/status////" rel="nofollow">https://ourapp.com/status////</a>&#34; then path would be &#34;/status////&#34;.</p> <p>If servers expects path to be &#34;/status/&#34; then your Java&#39;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&#39;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&#39;t do any of that.</p> <pre><code>&gt;&gt;&gt; import requests &gt;&gt;&gt; r = requests.get(&#39;https://ourapp.com/status&#39;, headers={&lt;your headers&gt;}) &gt;&gt;&gt; print r.url # print final URL &gt;&gt;&gt; print r.request.headers &gt;&gt;&gt; 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

455 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传