import ( "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/client" ) func main() { transport := runtime.NewClientWithTimeout("http://api.example.com", "/v1", []string{"http"}) client := client.New(transport, nil) request := client.Get().WithPath("/users/123") response, err := request.Do() if err != nil { // handle error } fmt.Println(response.StatusCode()) }
import ( "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/client" ) func main() { headers := runtime.Headers{} headers.Add("Authorization", "Bearer abc123") transport := runtime.NewClientWithTimeout("http://api.example.com", "/v1", []string{"http"}) client := client.New(transport, nil) request := client.Get().WithPath("/users/123").WithHeader("X-App-Id", "myapp").WithHeaders(headers) response, err := request.Do() if err != nil { // handle error } fmt.Println(response.StatusCode()) }In this example, we add a custom "Authorization" header to the request using the Headers type from the runtime package. We then create a new client and request as before, but this time we use the WithHeader() and WithHeaders() methods to set the "X-App-Id" and "Authorization" headers respectively. Overall, the go github.com.go-openapi.runtime ClientRequest is a useful tool for making HTTP requests in Go based on OpenAPI/Swagger API documentation. The package provides a flexible, type-safe way to create and send requests with support for custom headers, query parameters, and other HTTP features.