FAQ
Dear Gophers!

I'm having a weird issue with (reverse) proxying api requests to the github
api. I'm simply trying to log each request/response pair in a roundtripper,
but in the example below I left it out because that logic doesn't seem to
be the issue here:

package main

import (
"log"
"net/http"
"net/http/httputil"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
director := func(req *http.Request) {
req = r
req.URL.Scheme = "https"
req.URL.Host = "api.github.com"

log.Println(req.Method, req.URL.String())
log.Println(req.UserAgent())
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":8181", nil))
}

When I now issue a curl request to the proxy:

curl http://localhost:8181/orgs/docker

I receive a "We had issues producing the response to your request." from
Github, no json whatsoever. When I issue the curl command against github
directly it works withoug issue, i.e:

curl https://api.github.com/orgs/docker
{...}

I'm probably missing something obvious, anyone has a clue?

Thanks in advance!


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rjeczalik at Feb 26, 2015 at 6:42 pm

    On 26 February 2015 at 18:08, wrote:
    I'm probably missing something obvious, anyone has a clue?
    I'd compare raw requests produced by curl and your program e.g. using
    ngrep(8).

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Feb 26, 2015 at 6:47 pm
    You need to set the Host field on the http.Request too. (Also, a
    ReverseProxy is a handler, so you can get rid of the HandleFunc)

    director := func(req *http.Request) {
    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"

    log.Println(req.Method, req.URL.String())
    log.Println(req.UserAgent())
    }
    proxy := &httputil.ReverseProxy{
    Director: director,
    }

    log.Fatal(http.ListenAndServe(":8181", proxy))

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Advanderveer at Feb 26, 2015 at 9:24 pm
    Hey James,

    This did not do the trick (note i'm setting the req.Host):

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    director := func(req *http.Request) {
    req = r
    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"
    }

    proxy := &httputil.ReverseProxy{Director: director}
    proxy.ServeHTTP(w, r)
    })

    log.Fatal(http.ListenAndServe(":8181", nil))

    This didn't do the trick either (not including the req.Host):
    director := func(req *http.Request) {
    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    }

    proxy := &httputil.ReverseProxy{Director: director}

    log.Fatal(http.ListenAndServe(":8181", proxy))

    But you're suggestion works:

    director := func(req *http.Request) {
    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"
    }

    proxy := &httputil.ReverseProxy{Director: director}

    log.Fatal(http.ListenAndServe(":8181", proxy))

    It seems like the extra HandleFunc also influenced the unexpected
    behaviour, do you have any hints why this would be?


    On Thursday, February 26, 2015 at 7:47:24 PM UTC+1, James Bardin wrote:

    You need to set the Host field on the http.Request too. (Also, a
    ReverseProxy is a handler, so you can get rid of the HandleFunc)

    director := func(req *http.Request) {
    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"

    log.Println(req.Method, req.URL.String())
    log.Println(req.UserAgent())
    }
    proxy := &httputil.ReverseProxy{
    Director: director,
    }

    log.Fatal(http.ListenAndServe(":8181", proxy))
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Feb 26, 2015 at 9:44 pm

    On Thu, Feb 26, 2015 at 4:24 PM, wrote:

    Hey James,

    This did not do the trick (note i'm setting the req.Host):

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    director := func(req *http.Request) {
    req = r

    Don't assign r to req, it's already a copy of r. They're both pointers, so
    you're reassigning the address of "r", which is is the original request,
    to the proxy's request, so you're no longer modifying the request that's
    being sent.

    It would also work if you did

         *req = *r

    but that's already done for you in the ReverseProxy.


    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"
    }

    proxy := &httputil.ReverseProxy{Director: director}
    proxy.ServeHTTP(w, r)
    })

    log.Fatal(http.ListenAndServe(":8181", nil))
    You are also creating a new proxy for each request, which is unneeded.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Advanderveer at Feb 27, 2015 at 7:50 am
    Thanks James, makes sense - I just copied this snipped for reproducing the
    problem - apparently it was a crappy to begin with
    On Thursday, February 26, 2015 at 10:44:40 PM UTC+1, James Bardin wrote:



    On Thu, Feb 26, 2015 at 4:24 PM, <advand...@gmail.com <javascript:>>
    wrote:
    Hey James,

    This did not do the trick (note i'm setting the req.Host):

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    director := func(req *http.Request) {
    req = r

    Don't assign r to req, it's already a copy of r. They're both pointers,
    so you're reassigning the address of "r", which is is the original
    request, to the proxy's request, so you're no longer modifying the request
    that's being sent.

    It would also work if you did

    *req = *r

    but that's already done for you in the ReverseProxy.


    req.URL.Scheme = "https"
    req.URL.Host = "api.github.com"
    req.Host = "api.github.com"
    }

    proxy := &httputil.ReverseProxy{Director: director}
    proxy.ServeHTTP(w, r)
    })

    log.Fatal(http.ListenAndServe(":8181", nil))
    You are also creating a new proxy for each request, which is unneeded.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 26, '15 at 5:08p
activeFeb 27, '15 at 7:50a
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase