Skip to content

Commit

Permalink
Merge pull request #7 from green-api/dev
Browse files Browse the repository at this point in the history
Fixed UploadFile
  • Loading branch information
Amele9 authored Jul 17, 2023
2 parents 3d0aca6 + 8527a0d commit 8d13379
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"mime/multipart"
"net/http"
"os"
"strings"
)

func executeRequest(method, url string, data map[string]interface{}, filePath string) (map[string]interface{}, error) {
client := &http.Client{}

req := getRequest(method, url, data, filePath)
if strings.Contains(url, "UploadFile") {
req = getUploadFileRequest(method, url, filePath)
}

resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -99,6 +103,22 @@ func getRequest(method, url string, data map[string]interface{}, filePath string
return req
}

func getUploadFileRequest(method, url string, filePath string) *http.Request {
buf, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}

req, err := http.NewRequest(method, url, bytes.NewBuffer(buf))
if err != nil {
log.Fatal(err)
}

req.Header.Add("Content-Type", http.DetectContentType(buf))

return req
}

func getResponse(resp *http.Response) (map[string]interface{}, error) {
body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 8d13379

Please sign in to comment.