diff --git a/runtime/modules/starlarkhttp/starlarkhttp.go b/runtime/modules/starlarkhttp/starlarkhttp.go index 5fb8764e21..4fc297e6d7 100644 --- a/runtime/modules/starlarkhttp/starlarkhttp.go +++ b/runtime/modules/starlarkhttp/starlarkhttp.go @@ -386,6 +386,7 @@ func (r *Response) Struct() *starlarkstruct.Struct { "encoding": starlark.String(strings.Join(r.TransferEncoding, ",")), "body": starlark.NewBuiltin("body", r.Text), + "bytes": starlark.NewBuiltin("bytes", r.Bytes), "json": starlark.NewBuiltin("json", r.JSON), }) } @@ -414,6 +415,18 @@ func (r *Response) Text(thread *starlark.Thread, _ *starlark.Builtin, args starl return starlark.String(string(data)), nil } +// Bytes returns the raw data as bytes +func (r *Response) Bytes(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + data, err := io.ReadAll(r.Body) + if err != nil { + return nil, err + } + r.Body.Close() + // reset reader to allow multiple calls + r.Body = io.NopCloser(bytes.NewReader(data)) + return starlark.Bytes(data), nil +} + // JSON attempts to parse the response body as JSON func (r *Response) JSON(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var data interface{}