Using swagger
to generate HTTP clients, using the command:
swagger generate client <path to swagger.yaml> -A <name>
Try to structure auto-generated snippets, by go-swagger
to a folder like sdk
, or maybe in other repository.
func TestOurClient(t *testing.T) {
cfg := client.DefaultTransportConfig().WithHost("localhost:9090")
c := client.NewHTTPClientWithConfig(nil, cfg)
params := products.NewListProductsParams()
prod, err := c.Products.ListProducts(params)
if err != nil {
t.Fatal(err)
}
fmt.Printf("%#v", prod.GetPayload()[0])
}
Adding the correct content-type
headers for our swagger spec
. Debugging auto-generated code can be really painful.
rw.Header().Add("Content-Type","application/json")
First understanding CORS, we can use this blog post on medium here
Importing gorilla/handlers
and using the CORS method, to grant allowed origins
for localhost:3000
Here:
ch := gohandlers.CORS(gohandlers.AllowedOrigins([]string{"http://localhost:3000"}))
Wrapping the sm
router from the mux package from gorilla into the CORS handlers sm
More insights on http package