-
Notifications
You must be signed in to change notification settings - Fork 1
/
responses.go
37 lines (34 loc) · 1 KB
/
responses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func writeErrorResponse(w http.ResponseWriter, status int) error {
w.WriteHeader(status)
_, responseWriteErr := w.Write([]byte("Request Failed"))
if responseWriteErr != nil {
return fmt.Errorf("Response could not be written for inbound request")
}
return nil
}
func writeResponse(w http.ResponseWriter, resp *http.Response) error {
for responseHeaderkey, responseHeaderValues := range resp.Header {
responseHeaderValue := responseHeaderValues[0]
for i := 1; i < len(responseHeaderValues); i++ {
responseHeaderValue = responseHeaderValue + "," + responseHeaderValues[i]
}
w.Header().Add(responseHeaderkey, responseHeaderValue)
}
w.WriteHeader(resp.StatusCode)
var respBodyBytes []byte
if resp.Body != nil {
respBodyBytes, _ = ioutil.ReadAll(resp.Body)
}
resp.Body.Close()
_, responseWriteErr := w.Write(respBodyBytes)
if responseWriteErr != nil {
return fmt.Errorf("Response could not be written for inbound request")
}
return nil
}