MCPcopy
hub / github.com/ory/kratos / addBody

Method addBody

request/builder.go:113–147  ·  view source on GitHub ↗
(ctx context.Context, body interface{})

Source from the content-addressed store, hash-verified

111}
112
113func (b *Builder) addBody(ctx context.Context, body interface{}) (err error) {
114 ctx, span := b.deps.Tracer(ctx).Tracer().Start(ctx, "request.Builder.addBody")
115 defer otelx.End(span, &err)
116
117 if isNilInterface(body) {
118 return nil
119 }
120
121 if b.Config.TemplateURI == "" {
122 return errors.New("got empty template path for request with body")
123 }
124
125 tpl, err := b.readTemplate(ctx)
126 if err != nil {
127 return err
128 }
129
130 switch b.r.Header.Get("Content-Type") {
131 case ContentTypeForm:
132 if err := b.addURLEncodedBody(ctx, tpl, body); err != nil {
133 return err
134 }
135 case "":
136 b.r.Header.Set("Content-Type", ContentTypeJSON)
137 fallthrough
138 case ContentTypeJSON:
139 if err := b.addJSONBody(ctx, tpl, body); err != nil {
140 return err
141 }
142 default:
143 return errors.New("invalid config - incorrect Content-Type for request with body")
144 }
145
146 return nil
147}
148
149func (b *Builder) addJSONBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error {
150 buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint))

Callers 1

BuildRequestMethod · 0.95

Calls 7

readTemplateMethod · 0.95
addURLEncodedBodyMethod · 0.95
addJSONBodyMethod · 0.95
isNilInterfaceFunction · 0.85
TracerMethod · 0.65
GetMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected