Performs an HTTP GET request and returns the response body and status code.
| Field | Type | Description |
|---|---|---|
URL | *string | Full URL to fetch |
| Field | Type | Description |
|---|---|---|
Body | string | Response body as string |
StatusCode | int | HTTP status code |
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.
Fetches a JSON response from a public endpoint and prints the status code and a body preview.
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)
}