OPEN SWAG GO
Features

Code Snippets

The pkg/tryit/snippets package generates ready-to-use code examples for an HTTP request. Built-in generators cover curl, JavaScript, Go, and Python.

import "github.com/gopackx/open-swag-go/pkg/tryit/snippets"

Manager

NewManager returns a manager pre-loaded with the four built-in generators.

snippets_manager.go
mgr := snippets.NewManager() // registers curl, javascript, go, python
 
snippet, ok := mgr.Generate("curl", snippets.Request{
	Method:  "POST",
	URL:     "https://api.example.com/users",
	Headers: map[string]string{"Content-Type": "application/json"},
	Body:    `{"name": "Alice", "email": "alice@example.com"}`,
})

The Request type describes the call to render:

type Request struct {
	Method      string
	URL         string
	Headers     map[string]string
	Body        string
	QueryParams map[string]string
}

Generating all languages

generate_all.go
all := mgr.GenerateAll(snippets.Request{
	Method: "GET",
	URL:    "https://api.example.com/users",
})
// all["curl"], all["javascript"], all["go"], all["python"]
 
languages := mgr.Languages() // ["curl", "javascript", "go", "python"]

Choosing languages in the console

Which snippet languages appear in the try-it console is controlled by the console configuration:

console_languages.go
import "github.com/gopackx/open-swag-go/pkg/tryit"
 
console := tryit.NewConsole(
	tryit.WithLanguages("curl", "javascript", "python"),
)

Example Output

Given a POST /users endpoint with a JSON body, the generators produce:

curl

curl_output
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"name": "Alice", "email": "alice@example.com"}'

JavaScript

javascript_output
const response = await fetch("https://api.example.com/users", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer <token>",
  },
  body: JSON.stringify({
    name: "Alice",
    email: "alice@example.com",
  }),
});
 
const data = await response.json();

Go

go_output
body := map[string]string{
	"name":  "Alice",
	"email": "alice@example.com",
}
jsonBody, _ := json.Marshal(body)
 
req, _ := http.NewRequest("POST", "https://api.example.com/users", bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer <token>")
 
resp, err := http.DefaultClient.Do(req)

Python

python_output
import requests
 
response = requests.post(
    "https://api.example.com/users",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer <token>",
    },
    json={"name": "Alice", "email": "alice@example.com"},
)
 
data = response.json()

Custom Generators

Register your own generator by implementing the Generator interface:

type Generator interface {
	Generate(req Request) string
	Language() string
	DisplayName() string
}
custom_generator.go
type RubyGenerator struct{}
 
func (RubyGenerator) Language() string    { return "ruby" }
func (RubyGenerator) DisplayName() string { return "Ruby" }
func (RubyGenerator) Generate(req snippets.Request) string {
	return "require 'net/http'\n# ..."
}
 
mgr := snippets.NewManager()
mgr.Register(RubyGenerator{})

Full Example

full_snippets.go
package main
 
import (
	"fmt"
 
	"github.com/gopackx/open-swag-go/pkg/tryit/snippets"
)
 
func main() {
	mgr := snippets.NewManager()
 
	req := snippets.Request{
		Method:  "POST",
		URL:     "https://api.example.com/users",
		Headers: map[string]string{"Content-Type": "application/json"},
		Body:    `{"name": "Alice"}`,
	}
 
	for lang, code := range mgr.GenerateAll(req) {
		fmt.Printf("=== %s ===\n%s\n\n", lang, code)
	}
}