aboutsummaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-04-23 13:28:52 +0700
committerShulhan <ms@kilabit.info>2024-04-23 13:28:52 +0700
commit225b6372d0592c2291ff4b9301a68c80d4660d77 (patch)
tree0b3fd4a38e34b4069f53928f98ed9da6e76e8151 /lib/http
parentf319122b736346c6e99d3d2165d018fb61de7faa (diff)
downloadpakakeh.go-225b6372d0592c2291ff4b9301a68c80d4660d77.tar.xz
lib/http: allow all HTTP method to generate HTTP request with body
Althought the RFC 7231 says that no special defined meaning for a payload in GET, some implementation of HTTP API sometimes use GET with content type "application/x-www-form-urlencoded".
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/client.go79
-rw-r--r--lib/http/client_request.go29
2 files changed, 42 insertions, 66 deletions
diff --git a/lib/http/client.go b/lib/http/client.go
index 27c272d3..10ee1a2c 100644
--- a/lib/http/client.go
+++ b/lib/http/client.go
@@ -182,8 +182,8 @@ out:
// GenerateHTTPRequest generate [http.Request] from [ClientRequest].
//
// For HTTP method GET, CONNECT, DELETE, HEAD, OPTIONS, or TRACE; the
-// [ClientRequest.Params] value should be nil or [url.Values].
-// If its [url.Values], then the params will be encoded as query parameters.
+// [ClientRequest.Params] value [should be nil] or [url.Values].
+// If its not nil, it will be converted as rules below.
//
// For HTTP method PATCH, POST, or PUT; the [ClientRequest.Params] will
// converted based on [ClientRequest.Type] rules below,
@@ -197,6 +197,8 @@ out:
// body.
// - If Type is [RequestTypeJSON] and Params is not nil, the Params
// will be encoded as JSON in the body.
+//
+// [should be nil]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
func (client *Client) GenerateHTTPRequest(req ClientRequest) (httpReq *http.Request, err error) {
var (
logp = `GenerateHTTPRequest`
@@ -206,63 +208,46 @@ func (client *Client) GenerateHTTPRequest(req ClientRequest) (httpReq *http.Requ
body io.Reader
)
- switch req.Method {
- case RequestMethodGet,
- RequestMethodConnect,
- RequestMethodDelete,
- RequestMethodHead,
- RequestMethodOptions,
- RequestMethodTrace:
+ switch req.Type {
+ case RequestTypeNone, RequestTypeXML:
+ // NOOP.
+ case RequestTypeQuery:
if len(paramsEncoded) != 0 {
req.Path += `?` + paramsEncoded
}
- case RequestMethodPatch,
- RequestMethodPost,
- RequestMethodPut:
+ case RequestTypeForm:
+ if len(paramsEncoded) != 0 {
+ body = strings.NewReader(paramsEncoded)
+ }
- switch req.Type {
- case RequestTypeNone, RequestTypeXML:
- // NOOP.
+ case RequestTypeMultipartForm:
+ var (
+ paramsAsMultipart map[string][]byte
+ ok bool
+ )
- case RequestTypeQuery:
- if len(paramsEncoded) != 0 {
- req.Path += `?` + paramsEncoded
- }
+ paramsAsMultipart, ok = req.Params.(map[string][]byte)
+ if ok {
+ var strBody string
- case RequestTypeForm:
- if len(paramsEncoded) != 0 {
- body = strings.NewReader(paramsEncoded)
+ contentType, strBody, err = GenerateFormData(paramsAsMultipart)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
- case RequestTypeMultipartForm:
- var (
- paramsAsMultipart map[string][]byte
- ok bool
- )
-
- paramsAsMultipart, ok = req.Params.(map[string][]byte)
- if ok {
- var strBody string
-
- contentType, strBody, err = GenerateFormData(paramsAsMultipart)
- if err != nil {
- return nil, fmt.Errorf(`%s: %w`, logp, err)
- }
-
- body = strings.NewReader(strBody)
- }
+ body = strings.NewReader(strBody)
+ }
- case RequestTypeJSON:
- if req.Params != nil {
- var paramsAsJSON []byte
- paramsAsJSON, err = json.Marshal(req.Params)
- if err != nil {
- return nil, fmt.Errorf(`%s: %w`, logp, err)
- }
- body = bytes.NewReader(paramsAsJSON)
+ case RequestTypeJSON:
+ if req.Params != nil {
+ var paramsAsJSON []byte
+ paramsAsJSON, err = json.Marshal(req.Params)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
+ body = bytes.NewReader(paramsAsJSON)
}
}
diff --git a/lib/http/client_request.go b/lib/http/client_request.go
index 9c704c0e..454aeae1 100644
--- a/lib/http/client_request.go
+++ b/lib/http/client_request.go
@@ -21,31 +21,22 @@ type ClientRequest struct {
// This field is optional.
Header http.Header
- //
// Params define parameter to be send on request.
// This field is optional.
+ // It will converted based on Type rules below,
//
- // For Method GET, CONNECT, DELETE, HEAD, OPTIONS, or TRACE; the
- // params value should be nil or url.Values.
- // If its url.Values, then the params will be encoded as query
- // parameters.
- //
- // For Method PATCH, POST, or PUT; the Params will converted based on
- // Type rules below,
- //
- // * If Type is RequestTypeQuery and Params is url.Values it will be
- // added as query parameters in the Path.
- //
- // * If Type is RequestTypeForm and Params is url.Values it will be
- // added as URL encoded in the body.
+ // * If Type is [RequestTypeQuery] and Params is [url.Values] it
+ // will be added as query parameters in the Path.
//
- // * If Type is RequestTypeMultipartForm and Params is
- // map[string][]byte, then it will be converted as multipart form in
- // the body.
+ // * If Type is [RequestTypeForm] and Params is [url.Values] it
+ // will be added as URL encoded in the body.
//
- // * If Type is RequestTypeJSON and Params is not nil, the params will
- // be encoded as JSON in body using json.Encode().
+ // * If Type is [RequestTypeMultipartForm] and Params is
+ // map[string][]byte, then it will be converted as multipart form
+ // in the body.
//
+ // * If Type is [RequestTypeJSON] and Params is not nil, the params
+ // will be encoded as JSON in body using [json.Encode].
Params any
body io.Reader