github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/io/net/http/get.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	h "net/http"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // GET makes a GET request.
    13  // @param {Object | String} urlOrParam - Target url or parameters.
    14  // @param {String} [param.url] - Target url or parameters.
    15  // @param {Object} [param.headers] - HTTP headers
    16  // @return {Binary} - Response in binary format
    17  func GET(ctx context.Context, args ...core.Value) (core.Value, error) {
    18  	if err := core.ValidateArgs(args, 1, 1); err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	arg := args[0]
    23  
    24  	if err := core.ValidateType(arg, types.String, types.Object); err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	if arg.Type() == types.String {
    29  		return makeRequest(ctx, Params{
    30  			Method:  "GET",
    31  			URL:     values.ToString(arg),
    32  			Headers: nil,
    33  			Body:    nil,
    34  		})
    35  	}
    36  
    37  	return execMethod(ctx, h.MethodGet, args)
    38  }