Skip to content

Instantly share code, notes, and snippets.

@shiimaxx
Created March 27, 2021 11:13
Show Gist options
  • Save shiimaxx/dccfcdbd66b69ff84ab73d8ededdde61 to your computer and use it in GitHub Desktop.
Save shiimaxx/dccfcdbd66b69ff84ab73d8ededdde61 to your computer and use it in GitHub Desktop.
GitLab API v4 POST /projects/:id/uploads
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
)
func main() {
url := "https://gitlab.com/api/v4/projects/18630472/uploads"
fieldname := "file"
filename := "hello.txt"
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
body := &bytes.Buffer{}
mw := multipart.NewWriter(body)
fw, err := mw.CreateFormFile(fieldname, filename)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(fw, file)
if err != nil {
log.Fatal(err)
}
contentType := mw.FormDataContentType()
if err := mw.Close(); err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment