xgoprev/xmodules/httpx/request.go

50 lines
683 B
Go
Raw Normal View History

2024-06-06 00:13:12 +03:00
package httpx
import (
"github.com/d5/tengo/v2"
"net/http"
"io"
//"log"
)
type Request struct {
*http.Request
tengo.ObjectImpl
2024-06-06 00:13:12 +03:00
}
func (r *Request) TypeName() string {
return "http.Request"
2024-06-06 00:13:12 +03:00
}
func (r *Request) String() string {
return "http.Request{...}"
2024-06-06 00:13:12 +03:00
}
func (r *Request) IndexGet(
index tengo.Object,
) (tengo.Object, error) {
key, ok := tengo.ToString(index)
if !ok {
return nil, tengo.ErrInvalidIndexValueType
}
switch key {
case "url" :
return &URL{
URL: r.URL,
}, nil
case "body" :
bts, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
return &tengo.Bytes{
Value: bts,
}, nil
}
// Nothing found.
return nil, nil
}