示例#1
0
文件: utils.go 项目: xiocode/bingo
func encodeParams(params map[string]interface{}) (result string, err error) {
	if len(params) > 0 {
		values := url.Values{}
		for key, value := range params {
			values.Add(key, to.String(value))
		}
		result = values.Encode()
	}
	return
}
示例#2
0
文件: utils.go 项目: xiocode/bingo
func read_body(response *http.Response) (body string, err error) {
	var reader io.ReadCloser
	var contents []byte
	using_gzip := response.Header.Get("Content-Encoding")
	switch using_gzip {
	case "gzip":
		reader, err = gzip.NewReader(response.Body)
		if err != nil {
			return
		}
		defer reader.Close()
	default:
		reader = response.Body
	}

	contents, err = ioutil.ReadAll(reader)
	if err != nil {
		return
	}
	body = to.String(contents)
	return body, nil
}
示例#3
0
文件: weibo.go 项目: sh8ker/weigo
func encodeMultipart(params map[string]interface{}) (multipartContentType string, multipartData *bytes.Buffer, err error) {
	if len(params) > 0 {
		multipartData = new(bytes.Buffer)
		bufferWriter := multipart.NewWriter(multipartData) // type *bytes.Buffer
		defer bufferWriter.Close()
		for key, value := range params {
			switch value.(type) {
			case *os.File:
				var picdata io.Writer
				picdata, err = bufferWriter.CreateFormFile(key, value.(*os.File).Name())
				multipartContentType = bufferWriter.FormDataContentType()
				if err != nil {
					return "", nil, err
				}
				io.Copy(picdata, value.(*os.File))
			default:
				bufferWriter.WriteField(key, to.String(value))
			}
		}
		return
	}
	return
}
示例#4
0
文件: weibo.go 项目: sh8ker/weigo
func (http *HttpObject) call(uri string, params map[string]interface{}, result interface{}) (err error) {
	var body string
	var url = fmt.Sprintf("%s%s.json", http.client.api_url, uri)
	if http.client.is_expires() {
		err = &APIError{When: time.Now(), ErrorCode: 21327, Message: "expired_token"}
		return err
	}
	body, err = httpCall(url, http.method, http.client.access_token, params)
	if err != nil {
		return
	}
	if strings.Trim(body, " ") == "" {
		return errors.New("Nothing Return From Http Requests!")
	}
	jsonbody, err := simplejson.NewJson([]byte(body))
	if err != nil {
		return
	}
	_, ok := jsonbody.CheckGet("error_code")
	if ok {
		err = &APIError{When: time.Now(), ErrorCode: to.Int64(jsonbody.Get("error_code")), Message: to.String(jsonbody.Get("error"))}
		return
	}
	err = JSONParser(body, result)
	if err != nil {
		return
	}
	return nil
}