Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the headers of HTTP response and send it back in next HTTP request

Go version: go1.8.1 windows/amd64

Sample code for HTTP request is:

func (c *Client) RoundTripSoap12(action string, in, out Message) error {
    fmt.Println("****************************************************************")
    headerFunc := func(r *http.Request) {
        r.Header.Add("Content-Type", fmt.Sprintf("text/xml; charset=utf-8"))
        r.Header.Add("SOAPAction", fmt.Sprintf(action))
        r.Cookies()
    }
    return doRoundTrip(c, headerFunc, in, out)
}

func doRoundTrip(c *Client, setHeaders func(*http.Request), in, out Message) error {
    req := &Envelope{
        EnvelopeAttr: c.Envelope,
        NSAttr:       c.Namespace,
        Header:       c.Header,
        Body:         Body{Message: in},
    }

    if req.EnvelopeAttr == "" {
        req.EnvelopeAttr = "http://schemas.xmlsoap.org/soap/envelope/"
    }
    if req.NSAttr == "" {
        req.NSAttr = c.URL
    }
    var b bytes.Buffer
    err := xml.NewEncoder(&b).Encode(req)
    if err != nil {
        return err
    }
    cli := c.Config
    if cli == nil {
        cli = http.DefaultClient
    }
    r, err := http.NewRequest("POST", c.URL, &b)
    if err != nil {
        return err
    }
    setHeaders(r)
    if c.Pre != nil {
        c.Pre(r)
    }
    fmt.Println("*************", r)
    resp, err := cli.Do(r)
    if err != nil {
        fmt.Println("error occured is as follows ", err)
        return err
    }
    fmt.Println("response headers are: ", resp.Header.Get("sprequestguid"))
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        // read only the first Mb of the body in error case
        limReader := io.LimitReader(resp.Body, 1024*1024)
        body, _ := ioutil.ReadAll(limReader)
        return fmt.Errorf("%q: %q", resp.Status, body)
    }
    return xml.NewDecoder(resp.Body).Decode(out)

I will call the RoundTripSoap12 function on the corresponding HTTP client. When I send a request for the first time I will be getting some headers in the HTTP response, so these HTTP response headers should be sent as-is in my next HTTP request.

like image 592
vijay Avatar asked Sep 19 '17 10:09

vijay


People also ask

How do I get HTTP response headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

Can you extract all the headers from response at run time?

If you want to retrieve all headers of response then use headers() or getHeaders() who return Headers object.

How do I fetch all headers from fetch call response?

You can't directly access the headers on the response to a fetch call – you have to iterate through after using the entries() method on the headers. Code sample (using react and looking for a query count) below: fetch(`/events? q=${query}&_page=${page}`) .

How do I send a header in HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.


1 Answers

You may be interested in the httputil package and the reverse proxy example provided if you wish to proxy requests transparently:

https://golang.org/src/net/http/httputil/reverseproxy.go

You can copy the headers from one request to another one fairly easily - the Header is a separate object, if r and rc are http.Requests and you don't mind them sharing a header (you may need to clone instead if you want independent requests):

rc.Header = r.Header // note shallow copy
fmt.Println("Headers", r.Header, rc.Header)

https://play.golang.org/p/q2KUHa_qiP

Or you can look through keys and values and only copy certain headers, and/or do a clone instead to ensure you share no memory. See the http util package here for examples of this - see the functions cloneHeader and copyHeader inside reverseproxy.go linked above.

like image 54
Kenny Grant Avatar answered Oct 18 '22 10:10

Kenny Grant