Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions runtime/modules/starlarkhttp/starlarkhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
Expand Down Expand Up @@ -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{}
Expand Down