Skip to content

Commit

Permalink
fix: fix nil body passing to NewRequestWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
0x9ef committed Dec 29, 2022
1 parent 0671fb2 commit 9580ce7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ func New(apiKey string) *Engine {
}

func (e *Engine) newReq(ctx context.Context, method string, url string, body any) (*http.Request, error) {
e.n++ // increment number of requests
var reader *bytes.Reader
if ctx == nil {
ctx = context.Background() // prevent nil context error
}
r := new(bytes.Reader)
if body != nil {
jsonb, err := json.Marshal(body)
if err != nil {
return nil, err
}
reader = bytes.NewReader(jsonb)
r = bytes.NewReader(jsonb)
}
req, err := http.NewRequestWithContext(ctx, method, url, reader)
req, err := http.NewRequestWithContext(ctx, method, url, r)
if err != nil {
return nil, err
}
// Setup Content-Type header only on POST operation
// Setup Content-Type=application/json header only on POST operation
if body != nil && method == http.MethodPost {
req.Header.Set("Content-type", "application/json")
}
Expand All @@ -57,6 +59,7 @@ func (e *Engine) newReq(ctx context.Context, method string, url string, body any
}

func (e *Engine) doReq(req *http.Request) (*http.Response, error) {
e.n++ // increment number of requests
resp, err := e.client.Do(req)
if err != nil {
return nil, err
Expand Down

0 comments on commit 9580ce7

Please sign in to comment.