net/http Request requires trailing slash

agolangf · 2016-12-27 11:01:42 · 519 次点击    
这是一个分享于 2016-12-27 11:01:42 的资源,其中的信息可能已经有所发展或是发生改变。

Hi Gophers,

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".

In every other language I use I can just send a request to https://ourapp.com/status with the appropriate header set and all is well.

What is confusing me is that in go I have to send the request to https://ourapp.com/status/ note the trailing slash.

Is this something specific to go that I just need to be aware of or am I seeing some oddity in JBoss/our application specific to how go handles requests?

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())
}

**评论:**

gohacker:

req.Header.Set("statuskey", "auth_key_redacted")

Probably issue 4800: https://github.com/golang/go/issues/4800

Try Go 1.8 beta 2.

hipone:

(net/http).Client with default transport is not doing any magic behind the scenes with your request - if your url is "https://ourapp.com/status/" then path part of it is "/status/" - accordingly if you used "https://ourapp.com/status////" then path would be "/status////".

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.

xandout:

So based on that it seems it isn't Client but the server.

Maybe my Chrome/python stuff is modifying the request.

ashwch:

You can debug this easily in Python using requests. requests preserves headers and also sets appropriates cookies based on the headers returned by the previous request, but Go won't do any of that.

>>> 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

If some sort of redirection is happening(based on the Location header returned by the above URL) then the above program will print https://ourapp.com/status/. You can also check the complete redirection history using r.history.

In Go you can do something similar by defining your own CheckRedirect function and printing the headers being sent with each redirect.

xandout:

Thanks for that tip, there is a 302 being sent for ourapp.com/status/. Based on my understanding of the docs, net/http will follow up to 10 redirects but it does not appear to be following this one.

Do I need to explicitly state that I would like to follow the 302?


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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