Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
azure-functions-cli(nodejs) で text/html がエラーになるので proxy 書いた
package main
import (
"io"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
res, err := Proxy(r)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
defer res.Body.Close()
if strings.HasPrefix(res.Header.Get("Content-Type"), "text") {
res.Header.Set("Content-Type", "text/html")
}
for key := range res.Header {
val := res.Header.Get(key)
w.Header().Set(key, val)
}
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
})
http.ListenAndServe(":17071", nil)
}
// Proxy ...
func Proxy(r *http.Request) (*http.Response, error) {
req, err := http.NewRequest(r.Method, "http://localhost:7071"+r.URL.Path, r.Body)
if err != nil {
return nil, err
}
for key := range r.Header {
val := r.Header.Get(key)
req.Header.Set(key, val)
}
client := &http.Client{}
return client.Do(req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.