Documentation ¶
Overview ¶
Sending request:
client := NewClient() var foo = struct { Bar string }{} var apiErr = struct { Message string }{} resp, err := client.Do(request, &foo, &apiErr)
Index ¶
- Constants
- Variables
- type Client
- type File
- type Request
- func DeleteRequest(rawUrl string) *Request
- func GetRequest(rawUrl string) *Request
- func HeadRequest(rawUrl string) *Request
- func NewRequest(rawUrl string) *Request
- func OptionsRequest(rawUrl string) *Request
- func PatchRequest(rawUrl string) *Request
- func PostRequest(rawUrl string) *Request
- func PutRequest(rawUrl string) *Request
- func (r *Request) Add(key string, value string) *Request
- func (r *Request) Body(body interface{}) *Request
- func (r *Request) Build() (*http.Request, error)
- func (r *Request) Del(key string) *Request
- func (r *Request) Field(key string, value string) *Request
- func (r *Request) File(name string, file File) *Request
- func (r *Request) Method(method string) *Request
- func (r *Request) Query(key string, value string) *Request
- func (r *Request) Set(key string, value string) *Request
Constants ¶
const (
//ErrCodeEmptyResponse ...
ErrCodeEmptyResponse = "EmptyResponseBody"
)
Variables ¶
var ErrEmptyResponseBody = bmxerror.New(ErrCodeEmptyResponse, "empty response body")
ErrEmptyResponseBody ...
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // The HTTP client to be used. Default is HTTP's defaultClient. HTTPClient *http.Client // Defaualt header for all outgoing HTTP requests. DefaultHeader http.Header }
Client is a REST client. It's recommend that a client be created with the NewClient() method.
func (*Client) Do ¶
Do sends an request and returns an HTTP response. The resp.Body will be consumed and closed in the method.
For 2XX response, it will be JSON decoded into the value pointed to by respv.
For non-2XX response, an attempt will be made to unmarshal the response into the value pointed to by errV. If unmarshal failed, an ErrorResponse error with status code and response text is returned.
type File ¶
type File struct { // File name Name string // File content Content io.Reader // Mime type, defaults to "application/octet-stream" Type string }
File represents a file upload in the POST request
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is a REST request. It also acts like a HTTP request builder.
func DeleteRequest ¶
DeleteRequest creates a REST request with DELETE method and the given rawUrl.
func GetRequest ¶
GetRequest creates a REST request with GET method and the given rawUrl.
func HeadRequest ¶
HeadRequest creates a REST request with HEAD method and the given rawUrl.
func NewRequest ¶
NewRequest creates a new REST request with the given rawUrl.
func OptionsRequest ¶
Creates a request with HTTP OPTIONS.
func PatchRequest ¶
PatchRequest creates a REST request with PATCH method and the given rawUrl.
func PostRequest ¶
PostRequest creates a REST request with POST method and the given rawUrl.
func PutRequest ¶
PutRequest creates a REST request with PUT method and the given rawUrl.
func (*Request) Add ¶
Add adds the key, value pair to the request header. It appends to any existing values associated with key.
func (*Request) Body ¶
Body sets the request body. Accepted types are string, []byte, io.Reader, or structs to be JSON encodeded.
func (*Request) File ¶
File appends a file upload item in the POST request. The file content will be consumed when building HTTP request (see Build()) and closed if it's also a ReadCloser type.