| | |
| | | return dat, nil |
| | | } |
| | | |
| | | func PostFormData(uri string, filename, paramName string, file multipart.File) (maps map[string]interface{}, err0 error) { |
| | | func PostFormData(uri string, filename, paramName string, fileBytes []byte) (maps map[string]interface{}, err0 error) { |
| | | body := &bytes.Buffer{} |
| | | writer := multipart.NewWriter(body) |
| | | _, err := writer.CreateFormFile(paramName, filename) |
| | |
| | | return nil, err |
| | | } |
| | | boundary := writer.Boundary() |
| | | //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary) |
| | | close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary)) |
| | | fmt.Println("PostFormData.fileLen:",len(fileBytes)) |
| | | file := bytes.NewBuffer(fileBytes) |
| | | request_reader := io.MultiReader(body, file, close_buf) |
| | | //_, err = io.Copy(part, file) |
| | | //writer.WriteField(key, val) |
| | | |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | timeout := time.Duration(5 * time.Second) //超时时间50ms |
| | |
| | | return dat |
| | | } |
| | | } |
| | | //解析http |
| | | func EsReq(method string, url string, parama []byte) (buf []byte, err error) { |
| | | timeout := time.Duration(10 * time.Second) |
| | | client := http.Client{ |
| | | Timeout: timeout, |
| | | } |
| | | request, err := http.NewRequest(method, url, bytes.NewBuffer(parama)) |
| | | request.Header.Set("Content-type", "application/json") |
| | | |
| | | if err != nil { |
| | | fmt.Println("build request fail !") |
| | | return nil, err |
| | | } |
| | | |
| | | resp, err := client.Do(request) |
| | | if err != nil{ |
| | | fmt.Println("request error: ", err) |
| | | return nil, err |
| | | } |
| | | |
| | | defer resp.Body.Close() |
| | | body, err := ioutil.ReadAll(resp.Body) |
| | | if err != nil { |
| | | fmt.Println(err) |
| | | return nil , err |
| | | } |
| | | return body, nil |
| | | } |