← Library I/ODeterministic

HTTPGetOp

Performs an HTTP GET request and returns the response body and status code.

Inputs

FieldTypeDescription
URL*stringFull URL to fetch

Outputs

FieldTypeDescription
BodystringResponse body as string
StatusCodeintHTTP status code

Typical Use Cases

  • Fetching external data from REST APIs
  • Reading remote configuration
  • Calling webhooks or health endpoints

Check the StatusCode. A non-200 status does not cause an error — use IfIntEqOp downstream to verify the response was successful before using the body.

Complete Runnable Example

Fetches a JSON response from a public endpoint and prints the status code and a body preview.

main.go
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"time"

	"github.com/akennis/sparsi-go/library"
	_ "github.com/akennis/dagor/operator/builtin"

	"github.com/panjf2000/ants/v2"
	"github.com/akennis/dagor"
	"github.com/akennis/dagor/graph"
)

func main() {
	url := "https://httpbin.org/json"
	library.RegisterConst("target_url", url)

	g, err := graph.NewBuilder("httpget_demo").
		Vertex("url_src").Op("target_url").
		Output("Result", "url_wire").

		Vertex("fetch").Op("HTTPGetOp").
		Input("URL", "url_wire").
		Output("Body", "response_body").
		Output("StatusCode", "status_code").

		Build()
	if err != nil {
		log.Fatalf("build: %v", err)
	}

	pool, err := ants.NewPool(4)
	if err != nil {
		log.Fatalf("pool: %v", err)
	}
	defer pool.Release()

	eng, err := dagor.NewEngine(g, pool)
	if err != nil {
		log.Fatalf("engine: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	if err := eng.Run(ctx); err != nil {
		log.Fatalf("run: %v", err)
	}

	out := map[string]any{
		"url": url,
	}
	if raw, ok := eng.GetOutput("status_code"); ok {
		out["status_code"] = *raw.(*int)
	}
	if raw, ok := eng.GetOutput("response_body"); ok {
		body := *raw.(*string)
		if len(body) > 80 {
			body = body[:80] + "..."
		}
		out["body_preview"] = body
	}

	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}