Exemplo n.º 1
1
func mwSet(mw *multipart.Writer, key, value string) error {
	pw, err := mw.CreateFormField(key)
	if err != nil {
		return err
	}
	if _, err = pw.Write([]byte(value)); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 2
0
func SendMediaRequest(client *twittergo.Client, reqUrl string, params map[string]string, media []byte) (mediaResp twittergo.MediaResponse, err error) {
	var (
		req         *http.Request
		resp        *twittergo.APIResponse
		body        io.ReadWriter = bytes.NewBufferString("")
		mp          *multipart.Writer
		writer      io.Writer
		contentType string
	)
	mp = multipart.NewWriter(body)
	for key, value := range params {
		mp.WriteField(key, value)
	}
	if media != nil {
		if writer, err = mp.CreateFormField("media"); err != nil {
			return
		}
		writer.Write(media)
	}
	contentType = fmt.Sprintf("multipart/form-data;boundary=%v", mp.Boundary())
	mp.Close()
	if req, err = http.NewRequest("POST", reqUrl, body); err != nil {
		return
	}
	req.Header.Set("Content-Type", contentType)
	if resp, err = client.SendRequest(req); err != nil {
		return
	}
	err = resp.Parse(&mediaResp)
	return
}
Exemplo n.º 3
0
func WriteFormData(bw *multipart.Writer, FormData map[string]string) error {
	for key, value := range FormData {
		formFieldWriter, err := bw.CreateFormField(key)
		if err != nil {
			return err
		}
		formFieldWriter.Write([]byte(value))
	}
	return nil
}
Exemplo n.º 4
0
// addField adds a field to the multipart writer
func addField(w *multipart.Writer, k, v string) error {
	fw, err := w.CreateFormField(k)
	if err != nil {
		return err
	}
	_, err = fw.Write([]byte(v))
	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 5
0
func addField(w *multipart.Writer, fieldName string, value []byte) error {

	fw, err := w.CreateFormField(fieldName)
	if err != nil {
		return err
	}

	_, err = fw.Write(value)

	return err

}
Exemplo n.º 6
0
func writeFileField(w *multipart.Writer, fieldsubid string, filePath string) error {
	ff, err := w.CreateFormField(fieldsubid)
	if err != nil {
		return err
	}
	file, err := os.Open(filePath)
	if err != nil {
		return err
	}
	if _, err := io.Copy(ff, file); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 7
0
// writeMultiPartParams writes the given parameters as parts of a multipart
// message using the given writer.
func writeMultiPartParams(writer *multipart.Writer, parameters url.Values) error {
	for key, values := range parameters {
		for _, value := range values {
			fw, err := writer.CreateFormField(key)
			if err != nil {
				return err
			}
			buffer := bytes.NewBufferString(value)
			io.Copy(fw, buffer)
		}
	}
	return nil

}