go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.98k stars 706 forks source link

Call to http.DetectContentType has trailing NUL bytes in buffer #754

Closed internetisalie closed 7 months ago

internetisalie commented 10 months ago

In writeMultipartFormFile, the call to http.DetectContentType is passed a buffer with potentially trailing 0 bytes. As a result, text/plain is never detected in such cases.

cbuf should be trimmed to size to ensure text/plain is property detected:

// Auto detect actual multipart content type
cbuf := make([]byte, 512)
size, err := r.Read(cbuf)
if err != nil && err != io.EOF {
  return err
}

partWriter, err := w.CreatePart(createMultipartHeader(fieldName, fileName, http.DetectContentType(cbuf[:size])))
if err != nil {
  return err
}
jeevatkm commented 7 months ago

@internetisalie Thanks for reporting it. I'm sorry for the delayed response.

It is addressed and will be part of the v2.12.0 release.