-
Notifications
You must be signed in to change notification settings - Fork 33
Tips, Tricks, and Gotchas
Scott Ganyo edited this page May 2, 2023
·
3 revisions
This page serves to hold helpful information that may not be easily categorized or doesn't seem to belong anywhere else.
The following test program uses the (deprecated) Discovery Format-based Go client to request a spec body. It fails in a surprising way.
package main
import (
"context"
"log"
apigeeregistry "google.golang.org/api/apigeeregistry/v1"
)
func main() {
ctx := context.TODO()
apigeeregistryService, err := apigeeregistry.NewService(ctx)
if err != nil {
log.Fatalf("%s", err)
}
result, err := apigeeregistryService.Projects.Locations.Apis.Versions.Specs.GetContents("projects/test/locations/global/apis/petstore/versions/v1/specs/openapi").Do()
if err != nil {
log.Fatalf("error: %s", err)
}
log.Printf("%T %+v", result, result)
}
If the spec body is JSON, it seems to run just fine, but returns an HttpBody
object with the ContentType
and Data
fields empty:
go run main.go
2023/04/04 09:59:47 *apigeeregistry.HttpBody &{ContentType: Data: Extensions:[] ServerResponse:{HTTPStatusCode:200 Header:map[Alt-Svc:[h3=":443"; ma=2592000,h3-29=":443"; ma=2592000] Content-Length:[4375] Content-Type:[application/x.openapi;version=3] Date:[Tue, 04 Apr 2023 16:59:47 GMT] Server:[ESF] Vary:[Origin X-Origin Referer] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN] X-Xss-Protection:[0]]} ForceSendFields:[] NullFields:[]}
If the spec body is something else (e.g. YAML), we see a more obvious error message:
$ go run main.go
2023/04/04 10:01:39 error: invalid character '#' looking for beginning of value
exit status 1
It appears that the Go client library is trying to interpret the HTTP response as a JSON struct containing the ContentType
and Data
fields. It doesn't complain when they are missing and ignores fields that it doesn't recognize, but "succeeds" as long as the response is valid JSON.