go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/fetch_json.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package funcs
     9  
    10  import (
    11  	"context"
    12  	"encoding/json"
    13  )
    14  
    15  // FetchJSON fetches a JSON from a given url.
    16  func FetchJSON(ctx context.Context, url, method, body string) (any, error) {
    17  	contents, err := fetchURLMethod(ctx, url, method, body)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	defer contents.Close()
    22  	var output any
    23  	if err := json.NewDecoder(contents).Decode(&output); err != nil {
    24  		return nil, err
    25  	}
    26  	return output, nil
    27  }