Пример #1
0
// getJSON fetches a URL and decodes the response body as JSON.
func getJSON(urlStr string, param web.Values) (interface{}, os.Error) {
	if param != nil {
		urlStr = urlStr + "?" + param.FormEncodedString()
	}
	r, err := http.Get(urlStr)
	if err != nil {
		return nil, err
	}
	defer r.Body.Close()
	if r.StatusCode != 200 {
		return nil, os.NewError(fmt.Sprint("Status ", r.StatusCode))
	}
	p, _ := ioutil.ReadAll(r.Body)
	var v interface{}
	err = json.NewDecoder(bytes.NewBuffer(p)).Decode(&v)
	if err != nil {
		return nil, err
	}
	return v, nil
}
Пример #2
0
// getUrlEncodedForm fetches a URL and decodes the response body as a URL encoded form.
func getUrlEncodedForm(urlStr string, param web.Values) (web.Values, os.Error) {
	if param != nil {
		urlStr = urlStr + "?" + param.FormEncodedString()
	}
	r, err := http.Get(urlStr)
	if err != nil {
		return nil, err
	}
	defer r.Body.Close()
	if r.StatusCode != 200 {
		return nil, os.NewError(fmt.Sprint("Status ", r.StatusCode))
	}
	p, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}
	m := make(web.Values)
	err = m.ParseFormEncodedBytes(p)
	if err != nil {
		return nil, err
	}
	return m, nil
}