github.com/gogf/gf/v2@v2.7.4/net/gclient/gclient_bytes.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gclient
     8  
     9  import (
    10  	"context"
    11  	"net/http"
    12  
    13  	"github.com/gogf/gf/v2/internal/intlog"
    14  )
    15  
    16  // GetBytes sends a GET request, retrieves and returns the result content as bytes.
    17  func (c *Client) GetBytes(ctx context.Context, url string, data ...interface{}) []byte {
    18  	return c.RequestBytes(ctx, http.MethodGet, url, data...)
    19  }
    20  
    21  // PutBytes sends a PUT request, retrieves and returns the result content as bytes.
    22  func (c *Client) PutBytes(ctx context.Context, url string, data ...interface{}) []byte {
    23  	return c.RequestBytes(ctx, http.MethodPut, url, data...)
    24  }
    25  
    26  // PostBytes sends a POST request, retrieves and returns the result content as bytes.
    27  func (c *Client) PostBytes(ctx context.Context, url string, data ...interface{}) []byte {
    28  	return c.RequestBytes(ctx, http.MethodPost, url, data...)
    29  }
    30  
    31  // DeleteBytes sends a DELETE request, retrieves and returns the result content as bytes.
    32  func (c *Client) DeleteBytes(ctx context.Context, url string, data ...interface{}) []byte {
    33  	return c.RequestBytes(ctx, http.MethodDelete, url, data...)
    34  }
    35  
    36  // HeadBytes sends a HEAD request, retrieves and returns the result content as bytes.
    37  func (c *Client) HeadBytes(ctx context.Context, url string, data ...interface{}) []byte {
    38  	return c.RequestBytes(ctx, http.MethodHead, url, data...)
    39  }
    40  
    41  // PatchBytes sends a PATCH request, retrieves and returns the result content as bytes.
    42  func (c *Client) PatchBytes(ctx context.Context, url string, data ...interface{}) []byte {
    43  	return c.RequestBytes(ctx, http.MethodPatch, url, data...)
    44  }
    45  
    46  // ConnectBytes sends a CONNECT request, retrieves and returns the result content as bytes.
    47  func (c *Client) ConnectBytes(ctx context.Context, url string, data ...interface{}) []byte {
    48  	return c.RequestBytes(ctx, http.MethodConnect, url, data...)
    49  }
    50  
    51  // OptionsBytes sends an OPTIONS request, retrieves and returns the result content as bytes.
    52  func (c *Client) OptionsBytes(ctx context.Context, url string, data ...interface{}) []byte {
    53  	return c.RequestBytes(ctx, http.MethodOptions, url, data...)
    54  }
    55  
    56  // TraceBytes sends a TRACE request, retrieves and returns the result content as bytes.
    57  func (c *Client) TraceBytes(ctx context.Context, url string, data ...interface{}) []byte {
    58  	return c.RequestBytes(ctx, http.MethodTrace, url, data...)
    59  }
    60  
    61  // RequestBytes sends request using given HTTP method and data, retrieves returns the result
    62  // as bytes. It reads and closes the response object internally automatically.
    63  func (c *Client) RequestBytes(ctx context.Context, method string, url string, data ...interface{}) []byte {
    64  	response, err := c.DoRequest(ctx, method, url, data...)
    65  	if err != nil {
    66  		intlog.Errorf(ctx, `%+v`, err)
    67  		return nil
    68  	}
    69  	defer func() {
    70  		if err = response.Close(); err != nil {
    71  			intlog.Errorf(ctx, `%+v`, err)
    72  		}
    73  	}()
    74  	return response.ReadAll()
    75  }