I would need to extend http package to achieve non standard response containing error description in status ie: 400 Required parameter is missing instead of 400 Bad request that is standard status description.
This is my actual implementation:
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
type GatewayHandler int
func main() {
var gh GatewayHandler
http.ListenAndServe(":9000", gh)
}
func (gh GatewayHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
legacyApiUrl := "http://some-url.com" + req.URL.RequestURI()
client := &http.Client{}
request, _ := http.NewRequest(req.Method, legacyApiUrl, nil)
response, _ := client.Do(request)
res.Header().Set("Status", response.Status)
for k, v := range response.Header {
fmt.Println(k, ": ", v)
i := ""
for _, j := range v {
i += j
}
res.Header().Set(k, i)
}
res.WriteHeader(response.StatusCode)
if response.Status != "200 OK" {
fmt.Println(response.Status)
}
result, _ := ioutil.ReadAll(response.Body)
output := string(result)
fmt.Println(output)
io.WriteString(res, output)
}
In general I need to forward that status from other URL that uses it and I need to keep it compatible.
Thank you very much in advance.
Jozef