MCPcopy
hub / github.com/kubernetes/client-go / Body

Method Body

rest/request.go:377–412  ·  rest/request.go::Request.Body

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{})

Source from the content-addressed store, hash-verified

375// If obj is a runtime.Object and nil, do nothing.
376// Otherwise, set an error.
377func (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.

Callers 15

UpdateMethod · 0.80
CreateMethod · 0.80
UpdateMethod · 0.80
DeleteMethod · 0.80
DeleteCollectionMethod · 0.80
PatchMethod · 0.80
CreateMethod · 0.80
UpdateMethod · 0.80
UpdateStatusMethod · 0.80
DeleteMethod · 0.80
DeleteCollectionMethod · 0.80
PatchMethod · 0.80

Calls 4

SetHeaderMethod · 0.95
glogBodyFunction · 0.85
ErrorfMethod · 0.65
EncodeMethod · 0.45

Tested by 11

TestRequestBodyFunction · 0.64
TestDoRequestNewWayFunction · 0.64
TestCheckRetryClosesBodyFunction · 0.64
TestDoRequestNewWayObjFunction · 0.64
TestDoRequestNewWayFileFunction · 0.64
TestWasCreatedFunction · 0.64
TestBodyFunction · 0.64