45 lines
818 B
Go
45 lines
818 B
Go
package httpx
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func NewJSONRequest(ctx context.Context, method, rawURL string, query url.Values, body any) (*http.Request, error) {
|
|
u := rawURL
|
|
if len(query) > 0 {
|
|
sep := "?"
|
|
if strings.Contains(rawURL, "?") {
|
|
sep = "&"
|
|
}
|
|
u += sep + query.Encode()
|
|
}
|
|
var reqBody io.Reader
|
|
if body != nil {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reqBody = bytes.NewReader(b)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, u, reqBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func SetHeaders(req *http.Request, headers map[string]string) {
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
}
|