Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Dec 7, 2023
1 parent 89ed8ca commit 5d70e28
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions utils_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package got
import (
"bytes"
"context"
"encoding/json"
"io"
"mime"
"net/http"
Expand All @@ -12,11 +13,18 @@ import (
// ReqMIME option type, it should be like ".json", "test.json", "a/b/c.jpg", etc
type ReqMIME string

// Req the url. The method is the http method, default value is "GET".
// If an option is http.Header, it will be used as the request header.
// If an option is [Utils.ReqMIME], it will be used to set the Content-Type header.
// Req is a helper method to send http request. It will handle errors automatically, so you don't need to check errors.
// The method is the http method, default value is "GET".
// If an option is [http.Header], it will be used as the request header.
// If an option is [ReqMIME], it will be used to set the Content-Type header.
// If an option is [context.Context], it will be used as the request context.
// Other option type will be treat as request body, it will be encoded by Utils.Write .
// Other option type will be treat as request body, it will be encoded by [Utils.Write].
// Some request examples:
//
// Req("GET", "http://example.com")
// Req("GET", "http://example.com", context.TODO())
// Req("POST", "http://example.com", map[string]any{"a": 1})
// Req("POST", "http://example.com", http.Header{"Host": "example.com"}, ReqMIME(".json"), map[string]any{"a": 1})
func (ut Utils) Req(method, url string, options ...interface{}) *ResHelper {
ut.Helper()

Expand Down Expand Up @@ -66,19 +74,19 @@ type ResHelper struct {
err error
}

// Bytes body
// Bytes parses body as [*bytes.Buffer] and returns the result
func (res *ResHelper) Bytes() *bytes.Buffer {
res.ut.Helper()
return res.ut.Read(res.Body)
}

// String body
// String parses body as string and returns the result
func (res *ResHelper) String() string {
res.ut.Helper()
return res.Bytes().String()
}

// JSON body
// JSON parses body as json and returns the result
func (res *ResHelper) JSON() (v interface{}) {
res.ut.Helper()
return res.ut.JSON(res.Body)
Expand Down

0 comments on commit 5d70e28

Please sign in to comment.