Merge pull request #12781 from itchyny/api-compile-json-content

Use pre-compiled regexp for matching Content-Type
This commit is contained in:
Kynan Ware 2026-03-02 10:16:37 -07:00 committed by GitHub
commit 0f5355e8b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -456,6 +456,8 @@ func apiRun(opts *ApiOptions) error {
return tmpl.Flush()
}
var jsonContentTypeRE = regexp.MustCompile(`[/+]json(;|$)`)
func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersWriter io.Writer, template *template.Template, isFirstPage, isLastPage bool) (endCursor string, err error) {
if opts.ShowResponseHeaders {
fmt.Fprintln(headersWriter, resp.Proto, resp.Status)
@ -469,7 +471,7 @@ func processResponse(resp *http.Response, opts *ApiOptions, bodyWriter, headersW
var responseBody io.Reader = resp.Body
defer resp.Body.Close()
isJSON, _ := regexp.MatchString(`[/+]json(;|$)`, resp.Header.Get("Content-Type"))
isJSON := jsonContentTypeRE.MatchString(resp.Header.Get("Content-Type"))
var serverError string
if isJSON && (opts.RequestPath == "graphql" || resp.StatusCode >= 400) {