Skip to content

Commit

Permalink
feat: response cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Sep 19, 2024
1 parent 9891ded commit d4a1bb4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions callx.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type Config struct {

// StreamResponseBody enables response body streaming.
StreamResponseBody bool

Cookies bool
}

// Interceptor the interface
Expand All @@ -111,8 +113,9 @@ type Interceptor interface {

// Response callx model
type Response struct {
Code int
Data []byte
Code int
Data []byte
Cookies map[string]string
}

// CallX the interface
Expand Down Expand Up @@ -200,10 +203,21 @@ func (n *callxMethod) request(urlStr string, method string, header Header, paylo

err := n.Client.Do(req, resp)
if err != nil {
return Response{Code: http.StatusNotFound, Data: []byte(err.Error())}
return Response{
Code: http.StatusNotFound,
Data: []byte(err.Error()),
}
}
setResponseInterceptor(resp, n.Config.Interceptor)

if n.Config.Cookies {
cookies := map[string]string{}
resp.Header.VisitAllCookie(func(key, value []byte) {
cookies[string(key)] = string(value)
})
return Response{Code: resp.StatusCode(), Data: resp.Body(), Cookies: cookies}
}

return Response{Code: resp.StatusCode(), Data: resp.Body()}
}

Expand Down

0 comments on commit d4a1bb4

Please sign in to comment.