Golang Request.Method Examples

Golang Request.Method - 30 examples found. These are the top rated real world Golang examples of net/http.Request.Method extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func wouldMatch(router *mux.Router, rq *http.Request, method string) bool {
	savedMethod := rq.Method
	rq.Method = method
	defer func() { rq.Method = savedMethod }()
	var matchInfo mux.RouteMatch
	return router.Match(rq, &matchInfo)
}
Example #2
0
func (this *GoChromeCastHttpServer) DownloadPlay(c web.C, w http.ResponseWriter, r *http.Request) {
	if params, err := utils.GetParams(r, "id", "file_path"); err != nil {
		w.Header().Set("Content-Type", "application/json")
		r := responses.JsonResponse{}
		r.Method = "DownloadPlay"
		r.Set(nil, err)
		io.WriteString(w, utils.RenderResponse(r))
	} else {
		if sc, display_path, err := this.ds.DownloadPlay(params["id"], params["file_path"]); err != nil {
			w.Header().Set("Content-Type", "application/json")
			r := responses.JsonResponse{}
			r.Method = "DownloadPlay"
			r.Set(nil, err)
			io.WriteString(w, utils.RenderResponse(r))
		} else {
			defer func() {
				if err == nil {
					sc.Close()
				}
			}()
			w.Header().Set("Content-Disposition", "attachment; filename=\""+params["file_path"]+"\"")
			http.ServeContent(w, r, display_path, time.Now(), sc)
		}
	}
}
Example #3
0
func main() {
	f, err := os.OpenFile("./file.exe", os.O_RDWR, 0666) //其实这里的 O_RDWR应该是 O_RDWR|O_CREATE,也就是文件不存在的情况下就建一个空文件,但是因为windows下还有BUG,如果使用这个O_CREATE,就会直接清空文件,所以这里就不用了这个标志,你自己事先建立好文件。
	if err != nil {
		panic(err)
	}
	stat, err := f.Stat() //获取文件状态
	if err != nil {
		panic(err)
	}
	f.Seek(stat.Size(), 0) //把文件指针指到文件末,当然你说为何不直接用 O_APPEND 模式打开,没错是可以。我这里只是试验。
	url := "http://dl.google.com/chrome/install/696.57/chrome_installer.exe"
	var req http.Request
	req.Method = "GET"
	//req.UserAgent = UA
	req.Close = true
	req.URL, err = http.ParseURL(url)
	if err != nil {
		panic(err)
	}
	header := http.Header{}
	header.Set("Range", "bytes="+strconv.Itoa64(stat.Size)+"-")
	req.Header = header
	resp, err := http.DefaultClient.Do(&req)
	if err != nil {
		panic(err)
	}
	written, err := io.Copy(f, resp.Body)
	if err != nil {
		panic(err)
	}
	println("written: ", written)
}
Example #4
0
// Put issues a PUT to the specified URL.
// Caller should close r.Body when done reading it.
func authPut(url_, user, pwd, client, clientURL, version, agent, bodyType string,
	body io.Reader) (r *http.Response, err error) {
	var req http.Request
	req.Method = "PUT"
	req.Body = body.(io.ReadCloser)
	if user != "" && pwd != "" {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
			"Authorization":          {"Basic " + encodedUsernameAndPassword(user, pwd)},
		}
	} else {
		req.Header = map[string][]string{
			"Content-Type":           {bodyType},
			"Transfer-Encoding":      {"chunked"},
			"User-Agent":             {agent},
			"X-Fluidinfo-Client":     {client},
			"X-Fluidinfo-Client-URL": {clientURL},
			"X-Fluidinfo-Version":    {version},
		}
	}

	req.URL, err = url.Parse(url_)
	if err != nil {
		return nil, err
	}

	return send(&req)
}
Example #5
0
// ServeHTTP servers the actual HTTP request by buidling a context and running
// it through all the handlers.
func (handler *HttpHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
	// override the method if needed
	method := request.Header.Get("X-HTTP-Method-Override")
	if method != "" {
		request.Method = method
	}

	// make the context
	ctx := webcontext.NewWebContext(responseWriter, request, handler.codecService)

	// copy the data
	for k, v := range handler.Data {
		ctx.Data()[k] = v
	}

	// run it through the handlers
	_, err := handler.Handlers.Handle(ctx)

	// do we need to handle an error?
	if err != nil {

		// set the error
		ctx.Data().Set(DataKeyForError, err)

		// tell the handler to handle it
		handler.ErrorHandler().Handle(ctx)

	}

}
Example #6
0
func Translate(from, to, s string) (t string, err error) {
	var url_ = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&format=text&q=" + url.QueryEscape(s) + "&langpair=" + url.QueryEscape(from) + "%7C" + url.QueryEscape(to)
	var r *http.Response
	if proxy := os.Getenv("HTTP_PROXY"); len(proxy) > 0 {
		proxy_url, _ := url.Parse(proxy)
		tcp, _ := net.Dial("tcp", proxy_url.Host)
		conn := httputil.NewClientConn(tcp, nil)
		var req http.Request
		req.URL, _ = url.Parse(url_)
		req.Method = "GET"
		r, err = conn.Do(&req)
	} else {
		r, err = http.Get(url_)
	}
	if err == nil {
		defer r.Body.Close()
		if b, err := ioutil.ReadAll(r.Body); err == nil {
			var r interface{}
			if err = json.NewDecoder(bytes.NewBuffer(b)).Decode(&r); err == nil {
				if r.(map[string]interface{})["responseStatus"].(float64) == 200 {
					return r.(map[string]interface{})["responseData"].(map[string]interface{})["translatedText"].(string), nil
				} else {
					err = errors.New(r.(map[string]interface{})["responseDetails"].(string))
				}
			}
		}
	}
	return "", err
}