Recipes/Go net/http

Go net/http

Create a job, with an idempotency key so a timeout costs nothing.

The code

type Column struct {
Name string `json:"name"`
Description string `json:"description"`
}
 
body, _ := json.Marshal(map[string]any{
"videos": []string{url},
"schema": map[string]any{
"multiple": true,
"columns": []Column{{
Name: "claim",
Description: "A factual claim the host states out loud",
}},
},
})
 
req, _ := http.NewRequest("POST",
"https://api.tubeextract.dev/v1",
bytes.NewReader(body))
 
req.Header.Set("X-API-Key", os.Getenv("TUBEEXTRACT_KEY"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", runID+":"+videoID)
 
resp, err := http.DefaultClient.Do(req)

Notes

The idempotency key is derived from the run and the video rather than generated fresh, so retrying after a timeout returns the original job instead of queueing the video twice. See retries and idempotency.

Was this page useful?Tell us what was missing