fixed bug with sending nil data in api requests

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-08-03 00:43:43 +01:00
parent 49ed2b05fd
commit 93a8794cd4
2 changed files with 4 additions and 4 deletions

6
api.go
View File

@ -1,8 +1,8 @@
package request
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
@ -20,7 +20,7 @@ type Response struct {
}
// API sends RESTful API requests
func API(method, url string, headers map[string]string, data *bytes.Buffer) (*Response, error) {
func API(method, url string, headers map[string]string, data io.Reader) (*Response, error) {
req, err := http.NewRequest(method, url, data)
if err != nil {
return &Response{method, http.StatusInternalServerError, req.URL, nil, err}, err
@ -47,7 +47,7 @@ func API(method, url string, headers map[string]string, data *bytes.Buffer) (*Re
}
// AsyncAPI send requests concurrently
func AsyncAPI(method, url string, headers map[string]string, data *bytes.Buffer, ch chan<- *Response, wg *sync.WaitGroup) {
func AsyncAPI(method, url string, headers map[string]string, data io.Reader, ch chan<- *Response, wg *sync.WaitGroup) {
defer wg.Done()
resp, _ := API(method, url, headers, data)
ch <- resp

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/michaellindman/request
module git.0cd.xyz/michael/request
go 1.14