From f39605706ac8aa09cadade0de85c838aa9decfc0 Mon Sep 17 00:00:00 2001 From: dorlolo <428192774@qq.com> Date: Mon, 9 May 2022 20:29:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simpleRequest.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/simpleRequest.go b/simpleRequest.go index ac25f32..07d7514 100644 --- a/simpleRequest.go +++ b/simpleRequest.go @@ -193,6 +193,48 @@ func (s *SimpleRequest) Get(urls string) (body []byte, err error) { return } +//通用的请求方法 +func (s *SimpleRequest) LaunchTo(urls, method string) (body []byte, err error) { + // body + s.initBody() + r, err := http.NewRequest(method, urls, s.body) + if err != nil { + return nil, err + } + //headers + for k := range s.headers { + r.Header[k] = append(r.Header[k], s.headers[k]...) + s.headers.Del(k) + } + //queryParams + r.URL.RawQuery = s.queryParams.Encode() + + body, err = s.do(r) + return +} + +func (s *SimpleRequest) Put(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodPut) +} +func (s *SimpleRequest) DELETE(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodDelete) +} +func (s *SimpleRequest) PATCH(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodPatch) +} +func (s *SimpleRequest) HEAD(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodHead) +} +func (s *SimpleRequest) CONNECT(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodConnect) +} +func (s *SimpleRequest) OPTIONS(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodOptions) +} +func (s *SimpleRequest) TRACE(url string) (body []byte, err error) { + return s.LaunchTo(url, http.MethodTrace) +} + // Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231. //func (s *SimpleRequest) Put(url string) (*Response, error) { // return r.Execute(MethodPut, url)