diff --git a/project_import_export.go b/project_import_export.go index 8d8f262eb..7ddef715d 100644 --- a/project_import_export.go +++ b/project_import_export.go @@ -18,14 +18,14 @@ package gitlab import ( "bytes" - "encoding/json" "fmt" "io" "mime/multipart" "net/http" "os" - "strings" "time" + + "github.com/google/go-querystring/query" ) // ProjectImportExportService handles communication with the project @@ -184,24 +184,28 @@ func (s *ProjectImportExportService) ImportFile(opt *ImportFileOptions, options return nil, nil, err } defer file.Close() - var b bytes.Buffer - writer := multipart.NewWriter(&b) - var optMap map[string]interface{} - optMarshalled, err := json.Marshal(opt) + + body := new(bytes.Buffer) + writer := multipart.NewWriter(body) + + fields, err := query.Values(opt) if err != nil { return nil, nil, err } - json.Unmarshal(optMarshalled, &optMap) - for key, value := range optMap { - if strings.ToLower(key) == "file" { + for name := range fields { + if name == "file" { part, err := writer.CreateFormFile("file", file.Name()) if err != nil { return nil, nil, err } - _, err = io.Copy(part, file) + if _, err = io.Copy(part, file); err != nil { + return nil, nil, err + } } else { - _ = writer.WriteField(strings.ToLower(key), fmt.Sprintf("%v", value)) + if err = writer.WriteField(name, fmt.Sprintf("%v", fields.Get(name))); err != nil { + return nil, nil, err + } } } @@ -209,7 +213,8 @@ func (s *ProjectImportExportService) ImportFile(opt *ImportFileOptions, options if err != nil { return nil, nil, err } - req, err := s.client.NewRequest(http.MethodPost, "projects/import", &b, options) + + req, err := s.client.NewRequest(http.MethodPost, "projects/import", body, options) if err != nil { return nil, nil, err }