go-resty / resty

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

support OnBeforeResponse #747

Open PeterYangs opened 11 months ago

PeterYangs commented 11 months ago
package main

import (
    "errors"
    "github.com/go-resty/resty/v2"
)

func main() {

    client := resty.New()

    client.OnBeforeResponse(func(client *resty.Client, response *resty.Response) error {

        if response.Size() > 1073741824 {

            return errors.New("file is too big")
        }

        return nil

    }).R().SetOutput("/path/123.zip").Get("http://www.ooxx.com/123.zip")

}

I want to determine the size of the content before parsing the Response, and throw an error if it exceeds my preset size.

gospider007 commented 10 months ago
package main

import (
  "errors"
  "github.com/go-resty/resty/v2"
)

func main() {

  client := resty.New()

  client.OnBeforeResponse(func(client *resty.Client, response *resty.Response) error {

      if response.Size() > 1073741824 {

          return errors.New("file is too big")
      }

      return nil

  }).R().SetOutput("/path/123.zip").Get("http://www.ooxx.com/123.zip")

}

I want to determine the size of the content before parsing the Response, and throw an error if it exceeds my preset size.

This can help you https://github.com/gospider007/requests/blob/master/test/middleware/requestCallback_test.go