github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/pkg/http/http.go (about)

     1  package http
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"github.com/coreos/etcd/log"
    11  )
    12  
    13  func DecodeJsonRequest(req *http.Request, data interface{}) error {
    14  	decoder := json.NewDecoder(req.Body)
    15  	if err := decoder.Decode(&data); err != nil && err != io.EOF {
    16  		log.Warnf("Malformed json request: %v", err)
    17  		return fmt.Errorf("Malformed json request: %v", err)
    18  	}
    19  	return nil
    20  }
    21  
    22  func Redirect(hostname string, w http.ResponseWriter, req *http.Request) {
    23  	originalURL := req.URL
    24  	redirectURL, _ := url.Parse(hostname)
    25  
    26  	// we need the original path and raw query
    27  	redirectURL.Path = originalURL.Path
    28  	redirectURL.RawQuery = originalURL.RawQuery
    29  	redirectURL.Fragment = originalURL.Fragment
    30  
    31  	log.Debugf("Redirect to %s", redirectURL.String())
    32  	http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect)
    33  }