51 lines
832 B
Go
51 lines
832 B
Go
package httpx
|
|
|
|
import "net/http"
|
|
import "github.com/d5/tengo/v2"
|
|
import "io"
|
|
//import "encoding/json"
|
|
import tjson "github.com/d5/tengo/v2/stdlib/json"
|
|
|
|
type Response struct {
|
|
*http.Response
|
|
tengo.ObjectImpl
|
|
}
|
|
|
|
func (r *Response) TypeName() string {
|
|
return "http.Response"
|
|
}
|
|
|
|
func (r *Response) String() string {
|
|
return "http.Response{...}"
|
|
}
|
|
|
|
func (r *Response) IndexGet(
|
|
index tengo.Object,
|
|
) (tengo.Object, error) {
|
|
key, ok := tengo.ToString(index)
|
|
if !ok {
|
|
return nil, tengo.ErrInvalidIndexValueType
|
|
}
|
|
|
|
switch key {
|
|
case "body" :
|
|
bts, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &tengo.Bytes{
|
|
Value: bts,
|
|
}, nil
|
|
case "body_json" :
|
|
bts, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tjson.Decode(bts)
|
|
}
|
|
|
|
// Nothing found.
|
|
return nil, nil
|
|
}
|
|
|