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

Method addJSONBody

request/builder.go:149–185  ·  view source on GitHub ↗
(ctx context.Context, jsonnetSnippet []byte, body interface{})

Source from the content-addressed store, hash-verified

147}
148
149func (b *Builder) addJSONBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error {
150 buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint))
151 enc := json.NewEncoder(buf)
152 enc.SetEscapeHTML(false)
153 enc.SetIndent("", "")
154
155 if err := enc.Encode(body); err != nil {
156 return errors.WithStack(err)
157 }
158
159 vm, err := b.deps.JsonnetVM(ctx)
160 if err != nil {
161 return errors.WithStack(err)
162 }
163 vm.TLACode("ctx", buf.String())
164
165 res, err := vm.EvaluateAnonymousSnippet(
166 b.Config.TemplateURI,
167 string(jsonnetSnippet),
168 )
169 if err != nil {
170 // Unfortunately we can not use errors.As / errors.Is, see:
171 // https://github.com/google/go-jsonnet/issues/592
172 if strings.Contains(err.Error(), (&jsonnet.RuntimeError{Msg: "cancel"}).Error()) {
173 return errors.WithStack(ErrCancel)
174 }
175
176 return errors.WithStack(err)
177 }
178
179 rb := strings.NewReader(res)
180 if err := b.r.SetBody(io.NopCloser(rb)); err != nil {
181 return errors.WithStack(err)
182 }
183
184 return nil
185}
186
187func (b *Builder) addURLEncodedBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error {
188 buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint))

Callers 1

addBodyMethod · 0.95

Calls 8

ContainsMethod · 0.80
JsonnetVMMethod · 0.65
TLACodeMethod · 0.65
StringMethod · 0.65
EncodeMethod · 0.45
ErrorMethod · 0.45
SetBodyMethod · 0.45

Tested by

no test coverage detected