MCPcopy
hub / github.com/danielgtaylor/huma / writeResponse

Function writeResponse

huma.go:559–591  ·  view source on GitHub ↗
(api API, ctx Context, status int, ct string, body any)

Source from the content-addressed store, hash-verified

557}
558
559func writeResponse(api API, ctx Context, status int, ct string, body any) error {
560 if ct == "" {
561 // If no content type was provided, try to negotiate one with the client.
562 var err error
563 ct, err = api.Negotiate(ctx.Header("Accept"))
564 if err != nil {
565 notAccept := NewErrorWithContext(ctx, http.StatusNotAcceptable, "unable to marshal response", err)
566 ct = "application/json"
567 if ctf, ok := notAccept.(ContentTypeFilter); ok {
568 ct = ctf.ContentType(ct)
569 }
570
571 ctx.SetHeader("Content-Type", ct)
572 if e := transformAndWrite(api, ctx, http.StatusNotAcceptable, "application/json", notAccept); e != nil {
573 return e
574 }
575
576 return err
577 }
578
579 if ctf, ok := body.(ContentTypeFilter); ok {
580 ct = ctf.ContentType(ct)
581 }
582
583 ctx.SetHeader("Content-Type", ct)
584 }
585
586 if err := transformAndWrite(api, ctx, status, ct, body); err != nil {
587 return err
588 }
589
590 return nil
591}
592
593func writeResponseWithPanic(api API, ctx Context, status int, ct string, body any) {
594 if err := writeResponse(api, ctx, status, ct, body); err != nil {

Callers 2

WriteErrFunction · 0.85
writeResponseWithPanicFunction · 0.85

Calls 5

transformAndWriteFunction · 0.85
NegotiateMethod · 0.65
HeaderMethod · 0.65
ContentTypeMethod · 0.65
SetHeaderMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…