Skip to content

Commit

Permalink
fix: update to map of string
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnytg committed Oct 25, 2023
1 parent 1cc2a8a commit 00b555d
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions webreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ func (h *Headers) Add(key string, value string) {
type Request struct {
URL string
TimeOut int
Headers []H
Headers map[string]string
TypeRequest string
Body []byte
StatusCode int
}

var DatabaseHistory []Request

func Builder(method string) *Request {
r := Request{}

Expand All @@ -55,8 +53,8 @@ func (r *Request) SetTimeOut(timeOut int) *Request {
return r
}

func (r *Request) SetHeaders(headers Headers map[string]string) *Request {
r.Headers = headers.List
func (r *Request) SetHeaders(headers map[string]string) *Request {
r.Headers = headers
return r
}

Expand All @@ -77,8 +75,6 @@ func (r *Request) SetStatusCode(statusCode int) *Request {

func (r *Request) Execute() ([]byte, error) {

DatabaseHistory = append(DatabaseHistory, *r)

client := &http.Client{}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeOut)*time.Second)
Expand All @@ -95,10 +91,9 @@ func (r *Request) Execute() ([]byte, error) {
if err != nil {
return nil, err
}
for _, header := range r.Headers {
for key, value := range header {
request.Header.Add(key, value)
}

for key, value := range r.Headers {
request.Header.Add(key, value)
}

resp, err := client.Do(request)
Expand All @@ -107,8 +102,8 @@ func (r *Request) Execute() ([]byte, error) {
}
if resp != nil {
r.SetStatusCode(resp.StatusCode)
defer resp.Body.Close()
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 00b555d

Please sign in to comment.