imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

Retry Error in SetFileBytes(), using v3.42.1 #300

Closed KyoungnamMin closed 7 months ago

KyoungnamMin commented 7 months ago

go version: 1.21.3 req version: v3.42.1

I tried to send file bytes to the body using req SetFileBytes(). However, every time a retry was performed, I saw a case where the request failed. When I checked the server log receiving the request, the file byte length was 0.

My guess is that there is an error in the retry logic using the SetFileBytes() function. So, looking at the logic, I think that when retrying with roundTrip, the logic that sends the file seeker to the initial position of the file does not work. Below code is writeMultipartFormFile() function in middleware.go start 66 line. I think this logic should work, but does not work in SetFileBytes() cause it's type is io.ReadCloser.

if r.RetryAttempt > 0 { // reset file reader when retry a multipart file upload
  if rs, ok := content.(io.ReadSeeker); ok {
      _, err = rs.Seek(0, io.SeekStart)
      if err != nil {
          return err
      }
  }
}

Please comment if my guess is wrong. To solve the problem situation, we are temporarily executing SetFileBytes() every time with SetRetryHook().

imroc commented 7 months ago

Nice catch! Fixed in the latest release, try it!

KyoungnamMin commented 7 months ago

Thank you for quick response and resolve.