LoginSignup
3
2

More than 3 years have passed since last update.

GoでHTTPリクエストのデバッグ

Last updated at Posted at 2019-06-03

ヘッダにHost: example.comをつけているのに期待通りの動作にならなくて、少しハマったので、メモ。

まず実際に実行されたHTTPリクエストやサーバーからのレスポンスを確認するには、httputilのDumpRequest, DumpResponseが便利。

    dump, _ := httputil.DumpRequest(req, true)
    fmt.Printf("%q", dump)

    res, err := client.Do(req)

    dump2, _ := httputil.DumpResponse(res, true)
    fmt.Printf("%q", dump2)

結果、

req.Headers.Set("Host", "example.com")

では期待通りにヘッダがつけられていないことが判明。正解は、

req.Host = "example.com"

と書くのだそうだ。

参考: https://qiita.com/hnakamur/items/8a8ad0813eb577fc9018

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2