From e3fcfd70ba95a3672de9cc2ef3ffa6079a85f303 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 9 Oct 2024 18:00:06 +0300 Subject: [PATCH 1/2] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3c022c8a..fc72f5c9 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Currently we support Linux and macOS (_except some packages_). All packages have - [`rand`](https://kaos.sh/g/ek.v13/rand) — Package for generating random data - [`req`](https://kaos.sh/g/ek.v13/req) — Package simplify working with an HTTP requests - [`secstr`](https://kaos.sh/g/ek.v13/secstr) — Package provides methods and structs for working with protected (_secure_) strings +- [`setup`](https://kaos.sh/g/ek.v13/setup) — provides methods to install/unistall application as a service on the system - [`signal`](https://kaos.sh/g/ek.v13/signal) — Package provides methods for handling POSIX signals - [`sliceutil`](https://kaos.sh/g/ek.v13/sliceutil) — Package provides methods for working with slices - [`sortutil`](https://kaos.sh/g/ek.v13/sortutil) — Package provides methods for sorting slices From 43aee198f91bb3482d6ff561e65225cf28e4c48a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 11 Oct 2024 02:08:49 +0300 Subject: [PATCH 2/2] [req] Guess the value of the 'Content-Type' header based on the request body type --- CHANGELOG.md | 4 ++++ req/req.go | 28 ++++++++++++++++------------ version.go | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bc89d7e..d7117f2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### [13.6.1](https://kaos.sh/ek/13.6.1) + +- `[req]` Guess the value of the `Content-Type` header based on the request body type + ### [13.6.0](https://kaos.sh/ek/13.6.0) - `[setup]` Added package to install/uninstall application as a service diff --git a/req/req.go b/req/req.go index 0a8aa775..8624e7c6 100644 --- a/req/req.go +++ b/req/req.go @@ -590,12 +590,16 @@ func (e *Engine) doRequest(r Request, method string) (*Response, error) { r.URL += "?" + r.Query.Encode() } - bodyReader, err := getBodyReader(r.Body) + bodyReader, contentType, err := getBodyReader(r.Body) if err != nil { return nil, RequestError{ERROR_BODY_ENCODE, err.Error()} } + if r.ContentType == "" { + r.ContentType = contentType + } + req, err := createRequest(e, r, bodyReader) if err != nil { @@ -736,25 +740,25 @@ func createFormFile(w *multipart.Writer, fieldName, file string) (io.Writer, err return w.CreateFormFile(fieldName, filepath.Base(file)) } -func getBodyReader(body any) (io.Reader, error) { +func getBodyReader(body any) (io.Reader, string, error) { switch u := body.(type) { case nil: - return nil, nil + return nil, "", nil case string: - return strings.NewReader(u), nil + return strings.NewReader(u), CONTENT_TYPE_PLAIN, nil case io.Reader: - return u, nil + return u, CONTENT_TYPE_OCTET_STREAM, nil case []byte: - return bytes.NewReader(u), nil - default: - jsonBody, err := json.MarshalIndent(body, "", " ") + return bytes.NewReader(u), CONTENT_TYPE_OCTET_STREAM, nil + } - if err == nil { - return bytes.NewReader(jsonBody), nil - } + jsonBody, err := json.MarshalIndent(body, "", " ") - return nil, err + if err == nil { + return bytes.NewReader(jsonBody), CONTENT_TYPE_JSON, nil } + + return nil, "", err } func isURL(url string) bool { diff --git a/version.go b/version.go index 14137bf4..17d1a6be 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.6.0" +const VERSION = "13.6.1"