based on the Google Drive API docs the proper way of uploading a file is:
curl -v -H 'Authorization: Bearer mytoken' -F 'metadata={"name": "test3.jpeg"};type=application/json' -F file=@jpeg_image.jpeg 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart'
Now, I need to perform the same request from golang code, but I am having a hard time translating this to golang, here is the code I am using after several attempts:
// fileBytes are of type []byte
buffer := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(buffer)
multipartWriter.WriteField("metadata", "{\"name\": \"test3.jpef\"};type=application/json")
partHeader := textproto.MIMEHeader{}
partHeader.Add("Content-Type", "image/jpeg")
filePartWriter, _ := multipartWriter.CreatePart(partHeader)
io.Copy(filePartWriter, bytes.NewReader(fileBytes))
multipartWriter.Close()
googleDriveRequest, _ := http.NewRequest(http.MethodPost, "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", buffer)
googleDriveRequest.Header.Add("Authorization", "Bearer "+accessToken)
googleDriveRequest.Header.Add("Content-Type", multipartWriter.FormDataContentType())
googleDriveAPIResponse, googleDriveAPIError := lib.HttpClient.Do(googleDriveRequest)
Using curl the request succeeds :
{
"kind": "drive#file",
"id": "1DwsbQGP3-QtS5p0iQqRtAjcCCsAfxzGD",
"name": "test3.jpeg",
"mimeType": "image/jpeg"
}
With Golang, I get a 400:
{
\"error\": {
\"errors\": [
{
\"domain\": \"global\",
\"reason\": \"badContent\",
\"message\": \"Unsupported content with type: image/jpeg \"
}
],
\"code\": 400,
\"message\": \"Unsupported content with type: image/jpeg\"
}
}
I also tried with different values for partHeader.Add("Content-Type", "image/jpeg")
, I tried with application/x-www-form-urlencoded
as well, I also commented out this line, and let golang do the detection, but still getting the same error.
Any suggestions or ideas?
Regards,