How to read the response from a NewSingleHostReverseProxy

OscarRyz

I've created a reverse proxy like this:

func ProxyFunc(w http.ResponseWriter, r *http.Request) {
    u, err := url.Parse("http://mynetworkserverhere:8957/some/path/here/")
    if err == nil {
        proxy := httputil.NewSingleHostReverseProxy(u)
        proxy.ServeHTTP(w, r)
        //????

    } else {
        w.Write([]byte(err.Error()))
    }
}

And invoke it from main:

func main() {
   log.Printf("Starting...")
   http.HandleFunc("/", ProxyFunc)
   log.Fatal(http.ListenAndServe(":6060", nil))
}

And it works as expected from the client side, but I'd like to read the response from the proxy, how can I do that?

Tyson

The net/http package is really flexible. You can replace the default Transport of the ReverseProxy with your own, which just uses the DefaultTransport. Then you can intercept the RoundTrip call and do whatever you'd like.

package main

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

func main() {
    log.Printf("Starting...")
    http.HandleFunc("/", someFunc)
    log.Fatal(http.ListenAndServe(":6060", nil))
}

func someFunc(w http.ResponseWriter, r *http.Request) {

    // change the request host to match the target
    r.Host = "you.host.here:port"
    u, _ := url.Parse("http://you.host.here:port/some/path/")
    proxy := httputil.NewSingleHostReverseProxy(u)
    // You can optionally capture/wrap the transport if that's necessary (for
    // instance, if the transport has been replaced by middleware). Example:
    // proxy.Transport = &myTransport{proxy.Transport}
    proxy.Transport = &myTransport{}

    proxy.ServeHTTP(w, r)
}

type myTransport struct {
    // Uncomment this if you want to capture the transport
    // CapturedTransport http.RoundTripper
}

func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) {
    response, err := http.DefaultTransport.RoundTrip(request)
    // or, if you captured the transport
    // response, err := t.CapturedTransport.RoundTrip(request)

    // The httputil package provides a DumpResponse() func that will copy the
    // contents of the body into a []byte and return it. It also wraps it in an
    // ioutil.NopCloser and sets up the response to be passed on to the client.
    body, err := httputil.DumpResponse(response, true)
    if err != nil {
        // copying the response body did not work
        return nil, err
    }

    // You may want to check the Content-Type header to decide how to deal with
    // the body. In this case, we're assuming it's text.
    log.Print(string(body))

    return response, err
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ExecuteMultipleResponse; How to read and store Guids from the response

From Dev

How to read full response from HttpURLConnection?

From Dev

How to read Token from header of a response in angularjs

From Dev

How to read multiline response from FTP server?

From Dev

How read binary response from ServerXMLHTTP?

From Dev

How to read response code from TcpClient

From Dev

ExecuteMultipleResponse; How to read and store Guids from the response

From Dev

How to read the response from serial port

From Dev

How to read JSON response?

From Java

How do I read a response from Python Requests?

From Dev

How to read JSON error response from $http if responseType is arraybuffer

From Dev

How to read a response from a web service that returns a byte[]

From Dev

How to read response from a credit card over NFC

From Dev

RestAssured - How to read an array element from the json response

From Dev

how to read soap response header from JAX-WS

From Dev

How to read json response from node js server?

From Dev

Read XML Response From Page

From Dev

Read data from json response

From Dev

How to read a json response from a akka-http response entity using json4s

From Dev

How to read response headers in angularjs?

From Dev

How to read AJAX response variable?

From Dev

How to read response headers with $resource?

From Dev

How to read a JSON response in Unity?

From Dev

How to read Response, different from methods used in pojo class from API in Android

From Dev

How to read content from http 200 response from server built using python flask?

From Dev

Trying to read response from Web API call

From Dev

AngularJS - Cannot read response headers from $http

From Dev

jQuery read array from ajax response

From Dev

Read response headers from image error handler

Related Related

  1. 1

    ExecuteMultipleResponse; How to read and store Guids from the response

  2. 2

    How to read full response from HttpURLConnection?

  3. 3

    How to read Token from header of a response in angularjs

  4. 4

    How to read multiline response from FTP server?

  5. 5

    How read binary response from ServerXMLHTTP?

  6. 6

    How to read response code from TcpClient

  7. 7

    ExecuteMultipleResponse; How to read and store Guids from the response

  8. 8

    How to read the response from serial port

  9. 9

    How to read JSON response?

  10. 10

    How do I read a response from Python Requests?

  11. 11

    How to read JSON error response from $http if responseType is arraybuffer

  12. 12

    How to read a response from a web service that returns a byte[]

  13. 13

    How to read response from a credit card over NFC

  14. 14

    RestAssured - How to read an array element from the json response

  15. 15

    how to read soap response header from JAX-WS

  16. 16

    How to read json response from node js server?

  17. 17

    Read XML Response From Page

  18. 18

    Read data from json response

  19. 19

    How to read a json response from a akka-http response entity using json4s

  20. 20

    How to read response headers in angularjs?

  21. 21

    How to read AJAX response variable?

  22. 22

    How to read response headers with $resource?

  23. 23

    How to read a JSON response in Unity?

  24. 24

    How to read Response, different from methods used in pojo class from API in Android

  25. 25

    How to read content from http 200 response from server built using python flask?

  26. 26

    Trying to read response from Web API call

  27. 27

    AngularJS - Cannot read response headers from $http

  28. 28

    jQuery read array from ajax response

  29. 29

    Read response headers from image error handler

HotTag

Archive