ezhttp/request_builder.go
2024-09-11 19:19:00 +02:00

42 lines
664 B
Go

package ezhttp
import (
"io"
"net/http"
urlpkg "net/url"
)
type RequestBuilder struct {
URL *urlpkg.URL
Method string
Header http.Header
Body io.Reader
}
func NewRequestBuilder() *RequestBuilder {
return &RequestBuilder{
URL: &urlpkg.URL{},
Header: make(http.Header),
Body: nil,
}
}
func NewRequestBuilderFromRequest(r *http.Request) *RequestBuilder {
return &RequestBuilder{
URL: r.URL,
Method: r.Method,
Header: r.Header,
Body: r.Body,
}
}
func (b *RequestBuilder) Build() *http.Request {
r, err := http.NewRequest(b.Method, b.URL.String(), b.Body)
if err != nil {
return nil
}
r.Header = b.Header
return r
}