(ctx context.Context, body interface{})
| 111 | } |
| 112 | |
| 113 | func (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 | |
| 149 | func (b *Builder) addJSONBody(ctx context.Context, jsonnetSnippet []byte, body interface{}) error { |
| 150 | buf := bytes.NewBuffer(make([]byte, 0, b.bodySizeHint)) |
no test coverage detected