Skip to content

middleware improvement #973

@text-adi

Description

@text-adi

Discussed in #903

Originally posted by laggu November 6, 2024
I would like to suggest some features about resty middleware.

give both resty.Request, resty.Response to parameter of middleware

currently, middleware only can have a resty.Request or a resty.Response as a parameter.
I know we don't have response before we send request.
but it's only a data type that we can make an empty or default value.

setting resty.Response and return by middleware

if we can have resty.Response on request middleware, we can change the resty.Response value directly
As resty.Response has already been set up, It's possible to return without error before sending request

I think this changing will make middleware more powerful
caching middleware will be a good example of it.

an empty response without error will be problematic so developer should be aware of it
but I think this bug would be easily catched by developer who use this feature.

example

// Create a Resty Client
client := resty.New()

// Registering Request Middleware
client.OnBeforeRequest(func(c *resty.Client, req *resty.Request, resp *resty.Response) error {
    // Now you have access to Client and current Request and response object
    // manipulate it as per your need

    var data struct{
        Foo string `json:"foo"`
        Bar string `json:"bar"`
    }{
        Foo: "a",
        Bar: "b",
    }

    // should make some functions to resty.Response
    resp.SetBody(data)
    resp.Stop()

    return nil  // if its success otherwise return error
  })

// Registering Response Middleware
client.OnAfterResponse(func(c *resty.Client, req *resty.Request, resp *resty.Response) error {
    // Now you have access to Client and current Request and Response object
    // manipulate it as per your need

    return nil  // if its success otherwise return error
  })
```</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions