Body makes the request use obj as the body. Optional. If obj is a string, try to read a file of that name. If obj is a []byte, send it directly. If obj is an io.Reader, use it directly. If obj is a runtime.Object, marshal it correctly, and set Content-Type header. If obj is a runtime.Object and nil,
(obj interface{})
| 375 | // If obj is a runtime.Object and nil, do nothing. |
| 376 | // Otherwise, set an error. |
| 377 | func (r *Request) Body(obj interface{}) *Request { |
| 378 | if r.err != nil { |
| 379 | return r |
| 380 | } |
| 381 | switch t := obj.(type) { |
| 382 | case string: |
| 383 | data, err := ioutil.ReadFile(t) |
| 384 | if err != nil { |
| 385 | r.err = err |
| 386 | return r |
| 387 | } |
| 388 | glogBody("Request Body", data) |
| 389 | r.body = bytes.NewReader(data) |
| 390 | case []byte: |
| 391 | glogBody("Request Body", t) |
| 392 | r.body = bytes.NewReader(t) |
| 393 | case io.Reader: |
| 394 | r.body = t |
| 395 | case runtime.Object: |
| 396 | // callers may pass typed interface pointers, therefore we must check nil with reflection |
| 397 | if reflect.ValueOf(t).IsNil() { |
| 398 | return r |
| 399 | } |
| 400 | data, err := runtime.Encode(r.serializers.Encoder, t) |
| 401 | if err != nil { |
| 402 | r.err = err |
| 403 | return r |
| 404 | } |
| 405 | glogBody("Request Body", data) |
| 406 | r.body = bytes.NewReader(data) |
| 407 | r.SetHeader("Content-Type", r.content.ContentType) |
| 408 | default: |
| 409 | r.err = fmt.Errorf("unknown type used for body: %+v", obj) |
| 410 | } |
| 411 | return r |
| 412 | } |
| 413 | |
| 414 | // Context adds a context to the request. Contexts are only used for |
| 415 | // timeouts, deadlines, and cancellations. |