I have this function to execute http request
func do(r *http.Request) (response *http.Response, e error) {
r.Header.Set(SessionHeader, client.SessionId)
response, e = client.Do(r)
if e != nil {
return nil, e
}
if response.StatusCode == http.StatusConflict {
client.SessionId = response.Header.Get(SessionHeader)
r.Header.Set(SessionHeader, client.SessionId)
response, e = client.Do(r)
if e != nil {
return nil, e
}
return
} else if response.StatusCode != http.StatusOK {
return nil, errors.New("status Not OK")
}
return
}
So I make a request, than if my session id is not valid, I set header from response and try again. The issue is that the Body got flushed when I do this and this error pops up:
Post http://localhost/api: http: ContentLength=99 with Body length 0
How can I workaround this problem?
Create a request and use it.
func do(r *http.Request) (response *http.Response, err error) {
r.Header.Set(SessionHeader, client.SessionId)
response, err = client.Do(r)
if err != nil {
return nil, err
}
if response.StatusCode == http.StatusConflict {
// Get SessionId
client.SessionId = response.Header.Get(SessionHeader)
// Create Request
req, err := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("SessionHeader", client.SessionId)
response, err := client.Do(req)
if err != nil {
return nil, err
}
return
} else if response.StatusCode != http.StatusOK {
return nil, errors.New("status Not OK")
}
return
}