-
Notifications
You must be signed in to change notification settings - Fork 6
/
uploadfile.go
68 lines (57 loc) · 1.45 KB
/
uploadfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package vcodeapi
import (
"bytes"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/brian1917/vcodeHMAC"
)
// Creates a new file upload http request with optional extra params
func uploadFile(credsFile, appID, sandboxID, path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
writer.WriteField("app_id", appID)
writer.WriteField("sandbox_id", sandboxID)
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://analysiscenter.veracode.com/api/5.0/uploadfile.do", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
// Set authorization header and content-type header
authHeader, err := vcodeHMAC.GenerateAuthHeader(credsFile, req.Method, req.URL.String())
if err != nil {
return nil, err
}
req.Header.Set("Authorization", authHeader)
// Make HTTP Request
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Process response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.Status != "200 OK" {
return nil, errors.New("uploadfile.do call error: " + resp.Status)
}
// Return data and nil error
return data, nil
}