0

When trying to perform a PUT request to a pre-signed Minio URL using the golang httpClient library the following error is returned:

<Error><Code>MissingContentLength</Code><Message>You must provide the Content-Length HTTP header.</Message><Key>obj</Key><BucketName>bucket</BucketName><Resource>/bucket/obj</Resource><RequestId>REMOVED</RequestId><HostId>REMOVED</HostId></Error>

I'm trying to upload a file to the URL created by running the following on a connected minioClient:

minioClient.PresignedPutObject(context.Background(), "bucket", "obj", time.Second*60)

The code which is erroring is:

url := "http://pre-signed-url-to-bucket-obj"
fileName := "test.txt"

file, err := os.Open(fileName)
if err != nil {
    log.Fatal(err)
}
defer file.Close()

request, err := http.NewRequest(http.MethodPut, url, file)
if err != nil {
    log.Fatal("Error creating request:", err)
}

// Tried including and excluding explicit Content-Length add, doesn't change response

// fStat, err := file.Stat()
// if err != nil {
//   log.Fatal("Error getting file info:", err)
// }

// request.Header.Set("Content-Length", strconv.FormatInt(fStat.Size(), 10))

client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
    log.Fatal("Error performing request:", err)
}
defer resp.Body.Close()

content, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal("Error reading response:", err)
}

log.Println(string(content))

I've checked the Request and from what I'm able to tell Content-Length is being added.

A curl call with the --upload-file option specified will work:

curl -X PUT 'http://pre-signed-url-to-bucket-obj' --upload-file test.txt

I'm able to verify Content-Length is correctly added.

I would like to avoid a form as it does weird stuff to the obj on Minio's end.

Any help is much appreciated!

Cirrith
  • 147
  • 11
  • 1
    `ContentLength` is a field on the [`Request`](https://pkg.go.dev/net/http#Request) struct. – JimB Oct 19 '21 at 12:45

1 Answers1

0

Do this to explicit content-length explicitly:

request.ContentLength = fStat.Size()

I verified that the above code works with this fix

Krishna Srinivas
  • 1,620
  • 1
  • 13
  • 19